All lessons

Making Decisions

7 min · TypeScript

Run different code depending on whether something is true.

TypeScript · Making Decisions0/5 cleared

Enemy 1 of 5

This code checks if temperature is greater than 20 and prints Warm, otherwise Cold. Since temperature is 5, only the else block runs. Press Run to see it.

Why this works

An if runs its { } block only when the condition evaluates to true; else covers everything else, so exactly one of the two blocks runs. Compare values with > , < , >= , <= , and === (strict equality, checking both value and type).

TypeScript, like modern JavaScript, favors === over == for comparisons: === never tries to convert one type to another to force a match, so the result is always predictable. Because temperature here is explicitly typed as number, TypeScript also guarantees you can never accidentally compare it against something incompatible without at least being warned.

You
Memory-Leak Rat
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.