Template Literals & Null

Template Literals & Null: A TypeScript project with strictNullChecks enabled is like an attendant who asks 'is this floor empty or occupied?' before you board the elevator — try

A TypeScript project with strictNullChecks enabled is like an attendant who asks 'is this floor empty or occupied?' before you board the elevator — try to use a value that might be null/undefined without checking first, and you're stopped before you even board (at compile time). So if Java already has `NullPointerException` as an error, why is catching this ahead of time so valuable? Because Java's NPE explodes exactly when that line runs — usually in production or mid-test-run — and it's the source of what Tony Hoare himself called his 'billion dollar mistake'; strictNullChecks instead makes that same risk impossible to even write into the code. The concrete QA payoff: a test helper that finds an element with `document.querySelector` and calls `.click()` on it without checking first crashes mid-test with 'Cannot read property of null' in plain JavaScript or non-strict TypeScript; with strictNullChecks on, the compiler stops that same code in the IDE, demanding a null check first, before the test is ever run.

Micro Lab: TypeScript coding practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Null safety — end of NullPointerException

string | null — if null is possible, TypeScript forces check before use

user.profile?.phone — returns undefined if profile absent, no exception

value ?? "default" — only kicks in for null/undefined; 0 and "" pass through

Order the steps of writing null-safe code:

Determine if value can be null

Use string | null type

Use ?. for safe access or write if check

Provide default value with ??

What is the difference between console.log(zero || "default") and console.log(zero ?? "default") for const zero = 0?

|| outputs "default" (0 is falsy); ?? outputs 0 (only checks null/undefined)