Nullish coalescing operator: printing 0 where || gives -1
Values that count as false: 0, -0, ''
js
let number =0
console.log(number || '-1'); //0 is treated as false, so -1 is printed
// prints -1
console.log(number ?? '-1');
// prints 0As above, when you want to print 0, use the Nullish Coalescing Operator
- #Nullish Coalescing Operator
- #JavaScript