都是根据前面的值来判断最终返回前面的值还是后面的值。

a ?? b
a || b

判断的方法不同:
使用 ?? 时,只有 anull 或者 undefined 时才会返回 b;
使用 || 时,a会先转化为布尔值判断,为 true 时返回a, false 返回b.

??更加适合在不知道变量是否有值时使用

// ??
undefined ?? 2	        // 2
null ?? 2               // 2
0 ?? 2			// 0
"" ?? 2			// ""
true ?? 2		// true
false ?? 2		// false

// ||
undefined || 2	        // 2
null || 2		// 2
0 || 2			// 2
"" || 2			// 2
true || 2		// true
false || 2		// 2
 posted on 2023-04-18 10:45  还能不能行d  阅读(407)  评论(0编辑  收藏  举报