loading...

re: Use JavaScript Optional Chaining, Today! VIEW POST

TOP OF THREAD FULL DISCUSSION
re: what is the difference between x?.y?.z || 24 and x?.y?.z ?? 24
 

The main difference between the Logical OR (||), and Nullish Coalescing Operator (??) is the that (??) returns its right-hand side operand when its left-hand side operand is only null or undefined and doesn't respect the other falsy value (e.g. 0, false, '').

See the outputs:

const x = { y: { z: 0 } } };
x?.y?.z || 24; 
// => 24

x?.y?.z ?? 24
// => 0

For sure, the behaviour for ?? will be exactly the same as || if z is undefiend or its value is null

Code of Conduct Report abuse