0Pricing
TypeScript Academy · Lesson

null, undefined, and Strict Null Checks

Handle null and undefined safely with strict mode.

null, undefined, and Strict Null Checks is a free TypeScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

In JavaScript, null and undefined are common sources of runtime errors. TypeScript's strict null checks force you to handle them explicitly.

null vs undefined

`undefined` means a variable has been declared but not assigned. `null` is an intentional absence of value. Both are separate types in TypeScript.
let a: undefined = undefined;
let b: null = null;

Without Strict Null Checks

Without `strictNullChecks`, null and undefined are subtypes of every type. This is the default in older TypeScript and is generally unsafe.
// strictNullChecks: false (unsafe default)
let name: string = null; // allowed — dangerous!

Enabling Strict Null Checks

With `strictNullChecks: true` (included in `strict`), null and undefined are only assignable to their own types or explicit union types.
// strictNullChecks: true
let name: string = null; // Error!
let maybeName: string | null = null; // OK

Optional Properties

Object properties marked with `?` are implicitly `T | undefined`. You must handle the case where they are absent.
interface Config {
  host: string;
  port?: number; // number | undefined
}

const c: Config = { host: 'localhost' };
console.log(c.port?.toString()); // optional chaining

Narrowing Nullable Types

Use an if-check to narrow a union with null or undefined before accessing properties.
function greet(name: string | null): string {
  if (name === null) return 'Guest';
  return 'Hello, ' + name;
}

Optional Chaining (?.)

The `?.` operator short-circuits to `undefined` if the left side is null or undefined, avoiding a runtime crash.
const user = getUser();
const city = user?.address?.city; // undefined if any step is null

Nullish Coalescing (??)

The `??` operator returns the right side only when the left side is null or undefined — unlike `||` which triggers on any falsy value.
const port = config.port ?? 3000; // 3000 if port is null/undefined
const label = name ?? 'Anonymous';

Non-Null Assertion Operator (!)

The `!` postfix operator tells TypeScript you are certain a value is not null or undefined. Use it only when you are absolutely sure.
const el = document.getElementById('root')!;
el.textContent = 'Loaded'; // no null check needed

Returning null vs undefined

Prefer returning `null` to indicate an intentional empty result and `undefined` for missing/optional values. Be consistent within your codebase.
function findUser(id: number): User | null {
  return db.find(u => u.id === id) ?? null;
}

strictNullChecks Prevents the Billion Dollar Mistake

Tony Hoare called null his 'billion dollar mistake'. TypeScript's strictNullChecks prevents null/undefined crashes by making you handle these values explicitly.

Quick Check

What does the `??` (nullish coalescing) operator return on its right side?

Recap

Enable strictNullChecks to prevent null/undefined bugs. Use union types to allow nullable values, optional chaining (?.) to safely traverse, and nullish coalescing (??) to provide defaults.

Frequently asked questions

Is the “null, undefined, and Strict Null Checks” lesson free?

Yes — the full text of “null, undefined, and Strict Null Checks” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “null, undefined, and Strict Null Checks”?

Handle null and undefined safely with strict mode. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “null, undefined, and Strict Null Checks” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Primitive Types: string, number, boolean
  2. The any Type and Why to Avoid It
  3. unknown vs any: The Safer Choice
  4. null, undefined, and Strict Null Checks
← Back to TypeScript Academy