DEV Community

Discussion on: Use JavaScript Optional Chaining, Today!

Collapse
 
ismail9k profile image
Abdelrahman Ismail • Edited

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