Overview - Nullish coalescing with types
What is it?
Nullish coalescing is a way to provide a default value when a variable is null or undefined. In TypeScript, it helps handle cases where a value might be missing without confusing other falsy values like 0 or empty string. This operator looks like two question marks (??) and returns the right side only if the left side is null or undefined. It works well with TypeScript's type system to keep code safe and clear.
Why it matters
Without nullish coalescing, developers often use or (||) to provide defaults, but that treats many valid values like 0 or empty string as missing, causing bugs. Nullish coalescing solves this by only treating null or undefined as missing. This makes programs more reliable and easier to understand, especially when working with typed data where knowing exactly what values are allowed is important.
Where it fits
Before learning this, you should understand basic TypeScript types and how JavaScript handles null, undefined, and falsy values. After this, you can explore advanced TypeScript features like optional chaining, strict null checks, and type narrowing to write safer and cleaner code.