0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaScript学習ログ 三項演算子(条件演算子)

0
Posted at

今日やったこと

  • 三項演算子(条件演算子)の基本構文を学習
  • 値によって処理を分ける方法を理解
  • 関数内でも使えることを確認

今日の学び

if文よりも短く記述できる条件分岐の構文。

<記述方法>
ある条件 ? trueの時の処理 : falseの時の処理

// 数値かどうか判定して文字列を返す
const num = 1300;
const checkType = typeof num === "number" ? num.toLocaleString() : "数値を入力してください";
console.log(checkType); // "1,300"

// 関数内で三項演算子を使用
const checkNum = (num1, num2) => {
  return num1 + num2 > 100 ? "100より大きい" : "100以下です";
}
console.log(checkNum(40, 90)); // "100より大きい"

三項演算子は必ずif文で書き換えられる。
処理が複雑なときや、ネストするときはif文で記述した方が良い。

今日の気づき

  • 三項演算子は1行で書けるので短い条件分岐に便利
  • 複雑になる場合は可読性のためにif文を使う
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?