LoginSignup
1
0

More than 1 year has passed since last update.

??(null結合演算子)ハテナハテナで条件分岐[JavaScript]

Last updated at Posted at 2022-04-09
const foo = value ?? null;

こんな感じで条件分岐を行うことができます。
上記の場合の処理を言葉で書くと、

valueが存在する場合はvalueを返し、fooに代入する。
valueがnullまたはundefinedの場合はnullを返し、fooに代入する。

こんな感じですね。

const foo = value ? value : null;

この式の短い版みたいなイメージで使ってます。
ただしvalueがnullかundefinedの場合でないと右側の値へ分岐を行ってくれないので、
true/falseで処理を分岐させたい場合には使えません。

1
0
1

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