||) vs NullishMember-only story
JavaScript Fallback Values Explained: OR (||) vs Nullish (??)
H
ey Devs đź‘‹
JavaScript has a funny way of confusing developers — mostly because it gives us so many ways to do the same thing.
I got bored of writing if conditions everywhere, so I jumped into using the OR operator (||) and the nullish coalescing operator (??).
Most of the time, I was using these operators without fully understanding their true purpose. (Not really — but it makes the story more dramatic, hehe. )
Not a Medium member? You can read the full article for free by clicking here đź”—
When I was a fresher, I saw these operators used everywhere. So let’s properly understand what they actually do.
1. Logical OR operator (||)
The logical OR operator returns the first truthy value, or the last value if nothing is truthy.
Falsy values in JavaScript include:
false, 0, "", null, undefined, NaNExample
console.log("" || "default"); // "default"
console.log(0 || 42); // 42
console.log(null || "fallback"); // "fallback"Because 0 and "" are falsy, || replaces them.







