0Pricing
TypeScript Academy · Lesson

If/else, switch, loops; truthiness & strict checks

Use if/else, switch, and loop patterns effectively; avoid pitfalls with truthiness and prefer strict checks.

If/else, switch, loops; truthiness & strict checks is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Overview

Goal: Build confidence with if/else, switch, and loops. You will also learn when a value is merely truthy versus strictly equal, so your branches reflect intent.

If/else basics

If/else branches on boolean expressions. Prefer explicit comparisons over loose checks to avoid surprises.

const n: number = 3;

if (n > 0) {
  console.log("positive");
} else if (n === 0) {
  console.log("zero");
} else {
  console.log("negative");
}

switch with unions

switch works well with literal unions. Exhaustively handle cases to prevent fallthrough; add a default for safety.

const role: "admin" | "user" | "guest" = "admin";

switch (role) {
  case "admin":
    console.log("full access");
    break;
  case "user":
    console.log("limited access");
    break;
  default:
    console.log("guest view");
}

Loop patterns

Use classic for for indices and for...of to iterate values. Keep loops short and clear.

for (let i = 0; i < 3; i++) {
  console.log("i =", i);
}

let sum = 0;
for (const v of [1, 2, 3]) {
  sum += v;
}
console.log("sum =", sum);

Truthiness vs strict

A non-empty string like "0" is truthy, but it is not strictly equal to the number 0. Prefer === when meaning matters.

const x: any = "0";

if (x) {
  console.log("truthy");
}

if (x === 0) {
  console.log("number zero");
} else {
  console.log("not number zero");
}

Best practices

Best practices:

  • Prefer === / !== to avoid coercion.
  • Keep branches short; extract functions for clarity.
  • Use literal unions with switch for exhaustiveness.
  • Favor for...of over index loops when you do not need the index.

Truthiness check

Quick check: Which statement is TRUE about truthiness vs strict checks?

Recap & next

Recap: You used if/else, switch, and loops; and learned the difference between truthy values and strict equality. Next: functions—params, defaults, and returns.

Frequently asked questions

Is the “If/else, switch, loops; truthiness & strict checks” lesson free?

Yes — the full text of “If/else, switch, loops; truthiness & strict checks” is free to read here on the web, and the TypeScript Academy course includes 3 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 “If/else, switch, loops; truthiness & strict checks”?

Use if/else, switch, and loop patterns effectively; avoid pitfalls with truthiness and prefer strict checks. 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 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “If/else, switch, loops; truthiness & strict 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. If/else, switch, loops; truthiness & strict checks
  2. Functions: params, default/optional/rest; return types
  3. Arrow functions & contextual typing
← Back to TypeScript Academy