Tsallaka zuwa abun ciki
Justice Tech School

null, undefined and Strict Null Checks

With strictNullChecks, null and undefined are separate types. Use optional chaining ?. and nullish coalescing ?? to handle them, and avoid the non null assertion ! unless certain.

Example
const user: { name?: string } = {};
console.log(user.name?.toUpperCase());
console.log(user.name ?? "anonymous");
const maybe: string | null = null;
console.log(maybe === null ? "empty" : maybe.length);

Result

undefined
anonymous
empty

Exercise

Default when null or undefined.

name  "guest"

Quiz Test

What does ?. do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.