re: How TypeScript 3.7 Helps Quality VIEW POST

re: example for Nullish Coalescing doesn't really seem to explain why it's useful: "old way" would just be var calculator = someCalculator || new Calcu...

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.


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

code of conduct - report abuse