Skip to content
Conditionals
step 1/5

Reading — step 1 of 5

Learn

~3 min readControl Flow

Conditionals

Branching in TypeScript is JavaScript's if / else if / else — plus one genuinely TypeScript-flavored gift: the compiler follows your conditions and refines types as it goes.

if / else if / else

const n: number = 42;

if (n > 100) {
    console.log("big");
} else if (n > 10) {
    console.log("medium");
} else {
    console.log("small");
}

Conditions are checked top to bottom. The FIRST one that is true wins, and everything after it is skipped. That ordering rule is about to matter a lot.

Always ===, never ==

"5" == 5;   // true  — loose equality coerces types first
"5" === 5;  // false — strict equality: different types, not equal

== silently converts its operands before comparing ("type coercion"), which produces famous absurdities. Use === and !== exclusively. TypeScript's type narrowing also reasons far more cleanly about strict equality.

Type narrowing — the TypeScript superpower

function describe(value: string | number): string {
    if (typeof value === "string") {
        return `string of length ${value.length}`; // value is string here
    }
    return `number: ${value.toFixed(2)}`;          // value is number here
}

Inside the if, the compiler KNOWS value is a string, so .length is allowed. After the branch, only number remains. Narrowing means your runtime checks and your compile-time types work together — you never cast, you just check.

The ternary expression

const label = n % 2 === 0 ? "even" : "odd";

cond ? a : b is an expression — it produces a value, so you can assign it or return it. If the two branches have different types, TypeScript infers the union: cond ? "yes" : 0 has type string | number.

Order matters: the FizzBuzz trap

The classic task: divisible by 15 → FizzBuzz, by 3 → Fizz, by 5 → Buzz, otherwise the number itself.

// WRONG — 15 is divisible by 3, so this prints "Fizz" and stops:
if (n % 3 === 0) console.log("Fizz");
else if (n % 15 === 0) console.log("FizzBuzz"); // unreachable!

Because the first true branch wins, the most specific condition must come first:

if (n % 15 === 0) console.log("FizzBuzz");
else if (n % 3 === 0) console.log("Fizz");
else if (n % 5 === 0) console.log("Buzz");
else console.log(n);

(n % 15 === 0 is the same as "divisible by both 3 and 5" — you can also write n % 3 === 0 && n % 5 === 0.)

Your exercise

The starter reads one integer:

const n: number = Number(require('fs').readFileSync(0, 'utf-8').trim());

Implement FizzBuzz for that single number, printing exactly one line.

Two mistakes the grader will catch:

  • Wrong branch order — checking % 3 before % 15 makes input 15 print Fizz instead of the expected FizzBuzz.
  • Wrong capitalization — the expected strings are exactly Fizz, Buzz, and FizzBuzz: capital F, capital B, no space between them.

For a plain number like 7, console.log(n) prints 7 — exactly what the test expects.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…