Skip to content
Educora
Beginner13 min5 / 27

Conditions: if, else and switch

Teach your program to make decisions: `if`, `else if`, `else`, the ternary operator and `switch`.

Check yourself
In this lesson you will learn
  • Write branching code with if, else if and else
  • 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.

JavaScript
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
Change 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.

JavaScript
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:

JavaScript
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
Interactive
Loading simulation…
The && 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.

JavaScript
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.

JavaScript
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!
The empty case 'Saturday': shares the code of the next case — one answer for two values.
JavaScript
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
ConstructWhen to use it
if / else if / elseany conditions: ranges (score >= 90), several variables, complex checks
? :choosing one of two values in a single line
switchcomparing one variable with many exact values
Exercise

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.

Exercise · JavaScript
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 if block runs only when the condition is true; else runs otherwise.
  • In an else if chain 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 : b picks one of two values.
  • switch compares values with ===; end each case with break.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
With const x = 7;, what does if (x > 10) { console.log('A'); } else { console.log('B'); } print?