1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaScript演算子「??」

Last updated at Posted at 2022-12-23

演算子「??」------ first-defined

let temp = a ?? b; //左変数がnullかundefinedじゃない場合,temp = a. 逆の場合temp = b

//上式は以下と同じ意味:
let temp = (a != null && a != undefined) ? a : b;

演算子「||」との違い
「??」: null と undefined が判断基準.
「||」: true と false が判断基準.
例:

temp = false ?? 100;   //temp = false
temp = "" ?? 100;   //temp = ""
temp = 0 ?? 100;   //temp = 0
temp = undefined ?? 100; //temp = 100
temp = null ?? 100; //temp = 100

temp = false || 100;  //temp = 100
temp = "" || 100;   //temp = 100
temp = 0 || 100;   //temp = 100
temp = undefined || 100; //temp = 100
temp = null || 100; //temp = 100

例では,""と0がJavaScriptの自動型変換により,falseとして扱われる.

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?