DEV Community

Discussion on: How TypeScript 3.7 Helps Quality

Collapse
 
davinc profile image
Danijel Vincijanović • Edited

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
Collapse
 
boredcity profile image
boredcity

I thought I remembered something like this being the edge case where this was useful... Thank you, this seems like a better example!

Collapse
 
integerman profile image
Matt Eland

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.