I was also wondering what's the difference between those two and I've found it. Nullish Coalescing better handles cases when it comes to truthy/falsy values.
For example:
const a = 0
const b = a || 100
console.log(b) // 100
Same thing, but different result with Nullish Coalescing:
const a = 0
const b = a ?? 100
console.log(b) // 0
I personally hate the use of the || operator in this context, but that's mostly because my background is a C# one where I read the operator and think that b would be assigned to true since a and 100 would both be evaluated as booleans.
Obviously they wouldn't, but to me ?? is way more intuitive than the use of || in this context.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I was also wondering what's the difference between those two and I've found it. Nullish Coalescing better handles cases when it comes to truthy/falsy values.
For example:
Same thing, but different result with Nullish Coalescing:
I thought I remembered something like this being the edge case where this was useful... Thank you, this seems like a better example!
I personally hate the use of the
||
operator in this context, but that's mostly because my background is a C# one where I read the operator and think thatb
would be assigned totrue
sincea
and100
would both be evaluated as booleans.Obviously they wouldn't, but to me
??
is way more intuitive than the use of||
in this context.