DEV Community

Discussion on: JavaScript Amazing operators

Collapse
 
taufik_nurrohman profile image
Taufik Nurrohman

Keep in mind:

let a, b = 1;
a ?? b; // → 1
a || b; // → 1
Enter fullscreen mode Exit fullscreen mode
let a = null, b = 1;
a ?? b; // → 1
a || b; // → 1
Enter fullscreen mode Exit fullscreen mode
let a = false, b = 1;
a ?? b; // → false
a || b; // → 1
Enter fullscreen mode Exit fullscreen mode
let a = 0, b = 1;
a ?? b; // → 0
a || b; // → 1
Enter fullscreen mode Exit fullscreen mode
let a = "", b = 1;
a ?? b; // → ""
a || b; // → 1
Enter fullscreen mode Exit fullscreen mode
  • ?? for null and undefined only.
  • || for falsy values such as "", 0 and false but also includes null and undefined as well.
Collapse
 
capscode profile image
capscode

Hey Hi,
Thanks for adding this...
shall I add this to my blog ? what say ?

Collapse
 
taufik_nurrohman profile image
Taufik Nurrohman

It’s up to you.

Collapse
 
nikla profile image
Nikla • Edited

I think the post would benefit from also mentioning it. These two are easy to mix up and use incorrectly.