Codington
0 of 6 problems solved0%

null & undefined

6 exercises

When strict null checks are on, null and undefined must be handled explicitly. This helps you avoid crashes from missing values:

function greet(name: string | null) { if (name === null) return "Hello, guest!"; return `Hello, ${name}!`; }

Key operators for handling maybe-values:

  • Nullish coalescingvalue ?? defaultValue (fallback for null/undefined)
  • Optional chaininguser?.address?.city (safe nested property access)
const displayName = user.name ?? "Anonymous"; const city = user?.address?.city;

In this category, you'll:

  • Handle null and undefined with explicit checks
  • Use ?? for clean default values
  • Use ?. to safely access nested properties
  • Distinguish between nullish values and falsy values (0, "", false)

Best practice: Prefer small, clear checks. Don't rely on general truthy/falsy rules when you specifically want to treat null or undefined as missing.

CodingtonCodington

© 2025 Codington. Built with TypeScript, TanStack Start and a lot of console.log().