- Write branching code with
if,else ifandelse - Combine conditions with
&&and|| - Choose good situations for the ternary operator and
switch
You make dozens of decisions every day: if it's raining, you take an umbrella; if it's late, you call a taxi; if your balance is low, you put off a purchase. Programs make decisions like these too — with the help of conditional statements.
if: when a condition is true
After the keyword if you write a condition in parentheses, and inside the curly braces { } goes the code to run when the condition is true — a block. If the condition is false, the block is skipped and the program carries on.
const temperature = 28;
if (temperature > 25) {
console.log('It is hot. Drink some water!');
}
console.log('Have a nice day');▸ Expected output
It is hot. Drink some water! Have a nice day
temperature to 15 and run again: the first message disappears.else and else if
The else block runs when the condition is false. If there are more than two options, you add new conditions with else if. JavaScript checks the conditions from top to bottom and runs the block of the first true one — it ignores the rest.
const score = 78;
if (score >= 90) {
console.log('Excellent');
} else if (score >= 70) {
console.log('Good');
} else if (score >= 50) {
console.log('Satisfactory');
} else {
console.log('Try again');
}▸ Expected output
Good
Combining conditions
To combine several conditions we use logical operators: && (AND) requires both conditions to be true, and || (OR) requires at least one. Suppose a cinema gives a discount to people under 18 at weekends:
const age = 15;
const isWeekend = true;
if (age < 18 && isWeekend) {
console.log('Discount ticket: 3 AZN');
} else {
console.log('Regular ticket: 5 AZN');
}
const day = 'Sunday';
if (day === 'Saturday' || day === 'Sunday') {
console.log('No school today');
}▸ Expected output
Discount ticket: 3 AZN No school today
&& operator works like an AND gate: the output is true only when both inputs are true. Switch to the OR gate to compare it with ||.The ternary operator
When you just need to pick one of two values and store it, there is a short form: condition ? value1 : value2. If the condition is true, the result is value1, otherwise value2. It is called the ternary operator because it has three parts.
const hour = 20;
const greeting = hour < 12 ? 'Good morning' : 'Good evening';
console.log(greeting);
const items = 1;
console.log(`You have ${items} ${items === 1 ? 'item' : 'items'} in the basket`);▸ Expected output
Good evening You have 1 item in the basket
switch: many options
When you need to compare one variable with many exact values, switch is easier to read than a long else if chain. Each case checks one value (with strict comparison ===), and break exits the switch. If no case matches, the default block runs.
const day = 'Saturday';
switch (day) {
case 'Saturday':
case 'Sunday':
console.log('Weekend!');
break;
case 'Friday':
console.log('Almost weekend');
break;
default:
console.log('Work day');
}▸ Expected output
Weekend!
case 'Saturday': shares the code of the next case — one answer for two values.const light = 'yellow';
switch (light) {
case 'green':
console.log('Go');
case 'yellow':
console.log('Get ready');
case 'red':
console.log('Stop');
}▸ Expected output
Get ready Stop
| Construct | When to use it |
|---|---|
if / else if / else | any conditions: ranges (score >= 90), several variables, complex checks |
? : | choosing one of two values in a single line |
switch | comparing one variable with many exact values |
Print the grade for score: 90 and above — A, 75 and above — B, 60 and above — C, otherwise — F. Then use the ternary operator to print passed (if the score is 60 or more) or failed.
const score = 83;
// 1) print the grade: A, B, C or F
// 2) print passed or failed with the ternary operator▸ Expected output
B passed
Key points
- An
ifblock runs only when the condition is true;elseruns otherwise. - In an
else ifchain the first true condition wins, so the strictest one belongs at the top. &&needs both conditions to be true,||needs at least one.- The ternary operator
condition ? a : bpicks one of two values. switchcompares values with===; end eachcasewithbreak.
Check yourself
10 questions. Every correct answer earns XP.
const x = 7;, what does if (x > 10) { console.log('A'); } else { console.log('B'); } print?