← BYTES
JavaScript·Intermediate·Ref: 3.2.14
What is the key difference between the nullish coalescing operator `??` and the logical OR `||`?
Have a question about this video? Mention reference 3.2.14 when you contact us.
a.They are functionally identical in every case
b.`??` only works with numbers
c.`||` is newer syntax than `??`
d.`??` only falls back to its right-hand side when the left side is specifically null or undefined; `||` falls back for ANY falsy value (0, "", false, NaN too)
`0 || "default"` evaluates to "default" (since 0 is falsy) even though 0 might be a genuinely valid intended value; `0 ?? "default"` correctly evaluates to 0, since `??` only treats null/undefined as 'missing', not every falsy value -- exactly the distinction that motivated adding `??` to the language.
Find this on YouTube & Instagram
Every BYTES video is tagged with its reference number and topic — search any of these on YouTube or Instagram to find this one:
#BYTES#BYTES3214#JavaScript