1
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?

【三項演算子】

1
Posted at

三項演算子は 条件 ? 真の場合の値 : 偽の場合の値 という構造を持っています。 例えば、以下のような if...else 文を短縮できる!

// 通常の if...else
let message;
if (isAdmin) {
  message = "管理者です";
} else {
  message = "一般ユーザーです";
}
console.log(message);

⬇ 三項演算子で簡潔に書くと…

let message = isAdmin ? "管理者です" : "一般ユーザーです";
console.log(message);

この ?は「条件を評価して、真なら左側の値を返し、偽なら右側の値を返す」という動作になる。

1
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
1
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?