Skip to content
Nullish Coalescing기술 정리

Nullish coalescing operator: printing 0 where || gives -1

1 min readTech notesMoved from my Tistory blogRead the Korean original

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 0

As above, when you want to print 0, use the Nullish Coalescing Operator

  • #Nullish Coalescing Operator
  • #JavaScript

More in Tech notes