演算子「??」------ 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として扱われる.