Selection (if / else)

A program that always does exactly the same thing, no matter what, is a bit like a light with no switch. The moment a program has to choose — turn left or right, pass or fail, charge full price or give a discount — it needs a way to look at the situation and pick. That power to choose is called selection, and in almost every programming language it is spelled ifelse.

The idea is simple enough to say in one line. The program tests a condition — a question that comes back either true or false (a boolean) — and then:

Exactly one of the two blocks runs — never both, never neither. That is the whole heart of it; everything below is just this idea, explored.

Your first decision: are you old enough?

Say a website lets you sign up only if you are 13 or older. The program looks at your age, and the condition is the question "is age at least 13?", written age >= 13. Press Run and watch which branch fires — then change the age and Run it again.

const age: number = 15; if (age >= 13) { console.log("Welcome — you can sign up!"); } else { console.log("Sorry, you must be at least 13."); }

Read it out loud and it almost sounds like English: if age is greater-than-or-equal-to 13, print the welcome; otherwise, print the refusal. The { … } curly braces mark where each block starts and ends, and the condition in the round ( … ) brackets is the thing being tested.

The condition is a true/false question

A condition is built from a comparison. You already met one, >=. Here is the full set you will use every day — each one asks a yes/no question about two values and hands back a boolean:

A comparison isn't only for deciding — it produces a real true/false value you can print and look at. Run this to see the booleans that conditions are made of:

const score: number = 7; console.log("score === 10 :", score === 10); console.log("score !== 10 :", score !== 10); console.log("score > 5 :", score > 5); console.log("score < 5 :", score < 5); console.log("score >= 7 :", score >= 7); console.log("score <= 7 :", score <= 7);

Whatever a condition works out to — true or false — is exactly what the if uses to choose its branch.

Picturing it: the flowchart diamond

Programmers often draw selection as a flowchart. The condition is a diamond with one arrow coming in and two arrows going out: a true path and a false path. Whichever way the answer points, that is the block that runs — and then both paths join back together and the program carries on. Here is our age check drawn out:

Every if/else you ever write has this exact shape underneath: one question, two ways out, back to one road.

More than two choices: else if

Life isn't always two-way. A test grade might be A, B, C or Fail. You can chain conditions with else if: the program checks each one in order and stops at the first that is true, running that block and skipping the rest. A final plain else catches everything that fell through. Run it and try different marks:

const mark: number = 72; if (mark >= 80) { console.log("Grade A — outstanding!"); } else if (mark >= 70) { console.log("Grade B — great work."); } else if (mark >= 60) { console.log("Grade C — a solid pass."); } else { console.log("Not passed yet — keep going!"); }

Order matters. Because the checks run top to bottom and stop at the first hit, a mark of 85 matches mark >= 80 straight away and never even looks at the lower conditions. If you wrote the smallest test first, every passing mark would grab it and nobody would ever get an A.

A worked example: odd or even

A classic use of selection is checking whether a number is even. The trick is the remainder operator %, which gives what's left after dividing: n % 2 is 0 when n is even and 1 when it's odd. So the condition n % 2 === 0 asks "does 2 divide it exactly?"

function describe(n: number): void { if (n % 2 === 0) { console.log(n + " is even"); } else { console.log(n + " is odd"); } } describe(4); describe(7); describe(10); describe(0);

Notice how wrapping the decision in a function means we can ask the same question about many numbers without rewriting the if each time — selection and functions team up beautifully.

The else is optional! Sometimes you want to do something only when a condition holds, and otherwise just carry on quietly. Then you write an if on its own:

let price: number = 40; if (age < 16) { price = price - 10; // children's discount — only runs for under-16s } console.log("You pay: " + price);

If the shopper is 16 or older, the block is simply skipped and price stays as it was. No else needed.

Combining conditions: && and ||

Sometimes one test isn't enough. You can join booleans with and (&& — true only when both sides are true) and or (|| — true when at least one side is true). A theme-park ride might need you to be tall enough and with an adult:

const heightCm: number = 130; const withAdult: boolean = true; if (heightCm >= 120 && withAdult) { console.log("Enjoy the ride!"); } else { console.log("Sorry, not this time."); }

Swap && for || and the rule loosens to "tall enough OR with an adult". Choosing the right connector is choosing exactly what your program means.

The single most common beginner bug in a condition is writing = when you mean ===. They look almost the same but do completely different jobs:

So if (grade = 5) does not test whether grade is 5 — it sets grade to 5 and then treats that as the condition, which quietly breaks your logic (and TypeScript will usually refuse to compile it at all). Always use the triple equals in a condition:

if (grade = 5) { … } // BUG: = assigns, so this is not a real test if (grade === 5) { … } // correct: === compares

A handy memory hook: one equals gives a value, three equals checks a value.