Conditional Statements (if, else if, else)
Conditionals in TypeScript are identical to JavaScript's — if, else if, and else, with curly braces marking each block.
let age: number = 20;
if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are a minor");
}You are an adult
=== vs. ==, and a check JavaScript can't do
TypeScript keeps both of JavaScript's equality operators, and the same advice still applies: always prefer === over ==. But TypeScript's type checker adds something plain JavaScript never had — comparing two values whose types can't possibly be equal is flagged as an error before your code ever runs.
console.log(5 === 5); console.log(5 === "5");
script.ts(2,13): error TS2367: This condition will always return 'false' since the types 'number' and 'string' have no overlap.
That's genuinely what TypeScript's compiler reports — not something written to look plausible. In plain JavaScript, 5 === "5" would quietly evaluate to false and keep running; TypeScript catches that it's almost certainly a mistake and refuses to compile it at all.
Next: for loops — repeating code over every item in a collection.
Practice exercise
Set a variable score: number = 82. Print "Pass" if score is 60 or above, otherwise print "Fail".