📜  || 之间的区别和 ??在 js - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:44.827000             🧑  作者: Mango

代码示例1
// The main difference is that nullish coalescing(??) operator will only
// give the result as the right operand only if the left operand is either null or undefined.

// Whereas the OR(||) operator will give the result as right operand 
// for all the falsy values of the left operand.

const a = 0;
// a || 10 --> Will result in 10, as || operator considers 0 as falsy value and resulting the right side operand
console.log(`a || 10 = ${a || 10}`);
// a ?? 10 --> Will result in 0, as ?? operator considers 0 as truthy value and resulting the left side operand
console.log(`a ?? 10 = ${a ?? 10}`);