TypeScript Tutorial
TypeScript If Else
if runs a block when a condition is true. else and else if cover the other paths.
if runs a block
Write if, a condition in parentheses, then a brace-wrapped block. If the condition is true, the block runs. If it is false, TypeScript skips the block and continues after the closing brace.
Always use braces, even for one statement. A later edit that adds a second line without braces will not do what it looks like. This tutorial never omits them.
Example
const score: number = 72;
if (score >= 50) {
console.log("pass");
}Change score and compile in /typescript/try. At 49 the program prints nothing. That is a reason to add else.
else covers the rest
Pair else with an if when you need exactly one of two paths. One block runs. The other does not. There is no third option in a plain if/else.
Example
const score: number = 41;
if (score >= 50) {
console.log("pass");
} else {
console.log("fail");
}else if chains more tests
else if means: the previous tests were false, so try this one. TypeScript walks the chain from the top and runs the first block whose condition is true. A final else catches everything that did not match.
Order matters. Put the strictest test first. If you check score >= 50 beforescore >= 90, a 95 never reaches the A branch.
Example
const score: number = 82;
if (score >= 90) {
console.log("A");
} else if (score >= 80) {
console.log("B");
} else if (score >= 70) {
console.log("C");
} else {
console.log("below C");
}Nested if
An if inside another if is a nested if. Use it when the second question only makes sense after the first one is true. Indent the inner block so a person can see the shape.
Example
const age: number = 20;
const hasTicket: boolean = true;
if (age >= 18) {
if (hasTicket) {
console.log("enter");
} else {
console.log("buy a ticket");
}
} else {
console.log("too young");
}Two independent facts can often be one condition: if (age >= 18 && hasTicket). Nest when the inner else needs a different message.
Conditions you will write
| Need | Write |
|---|---|
| Equal | if (a === b) { ... } |
| Not equal | if (a !== b) { ... } |
| Both true | if (a && b) { ... } |
| Either true | if (a || b) { ... } |
Do not write if (x = 1). That assigns 1 to x and then treats it as true. Compare with ===. TypeScript’s usual equality is three equals, not two. == coerces types; this course uses ===.
Braces are not optional here
JavaScript lets you skip braces when the body is one statement. Skip them and a later line that looks nested is actually outside the if. Always open and close { }. Match them. Indent the body.
There is no elif keyword. The spelling is two words: else if. The hour-of-day chain in your head is the same shape as the grade chain above: test the smallest bound first, then the next, thenelse.
When if is the wrong tool
If you are picking among many exact labels — a menu choice, a day number, a string command — aswitch is often clearer. That is the next chapter. For ranges such as scores, stay withelse if.