Loading video player…

Choose One Path With else and else if

In this video, you followed a visitor’s age through the day-trip ticket desk’s rules and saw how JavaScript picks exactly one path. Here are the key points:

  • An else block comes right after an if block. It has no condition of its own. With just an if and an else, the else block runs whenever the if condition is false, so exactly one of the two blocks runs. In a chain with else if, it runs only when every condition above it is false.
  • Between the if and the else, each else if adds another check: a condition in parentheses and a block in braces.
  • JavaScript checks the conditions from top to bottom. It runs the first branch whose condition is true and skips the rest of the chain without checking them.
  • When two conditions are true for the same value, the one higher up wins. An age of 3 is under 5 and under 16, so the visitor gets free entry.
  • If no condition is true, the else block runs.
  • A line after the chain runs whichever branch ran, like any line after an if block.

The desk’s rule table:

Age Ticket
Under 5 Free entry
5 to 15 Child ticket
65 and over Senior ticket
Everyone else Adult ticket

Here’s the final script from the video:

Language: JavaScript Filename: ticket.js
const age = 30;

if (age < 5) {
  console.log("Free entry");
} else if (age < 16) {
  console.log("Child ticket");
} else if (age >= 65) {
  console.log("Senior ticket");
} else {
  console.log("Adult ticket");
}
console.log("Enjoy the trip");
Language: Shell
$ node ticket.js
Adult ticket
Enjoy the trip

The video changed the age and ran the script again each time. These are the results it showed:

age First true condition Output
30 none, so the else block runs Adult ticket, then Enjoy the trip
70 age >= 65 Senior ticket, then Enjoy the trip
3 age < 5 Free entry, then Enjoy the trip
5 age < 16 Child ticket, then Enjoy the trip

Next, in Separate ifs, else if, or a Nested if?, you’ll compare this chain with the other shapes a decision can take.

00:00 With an if, you can already run code only when a condition is true. Now picture the ticket desk for a day trip, where every visitor gets exactly one ticket.

00:11 The desk follows a published rule table. Visitors under 5 get in free. From 5 to 15, it’s a child ticket. At 65 or older, it’s a senior ticket. Everyone else pays for an adult ticket.

00:26 One if can guard one action. This desk needs one answer out of four, and never two at once.

00:34 Start smaller, with two kinds of ticket. Your script stores the visitor’s age, 30, in a constant. If the age is under 16, it logs a child ticket.

00:48 Below that block comes the keyword else, with its own block in braces. It has no condition of its own. Its block runs whenever the if’s condition is false.

01:01 Run the script in the terminal. Thirty isn’t under 16, so the condition is false. JavaScript skips the first block and runs the else block instead, and the terminal shows Adult ticket.

01:14 Exactly one of the two blocks runs, every time.

01:19 Two paths aren’t enough for the desk, though. Between the if and the else, you can add more checks. Each one is an else if, followed by a condition in parentheses and a block in braces.

01:32 The first check now asks about free entry, under 5. The child ticket moves down into an else if. A second else if checks for 65 or older.

01:46 The else at the bottom still catches the adults. After the chain, one more line wishes the visitor a good trip. Run it again, and follow the age 30 down the chain.

01:58 Is it under 5? No, so JavaScript moves on. Is it under 16? No again. Is it 65 or older? Also no. Every condition was false, so the else block runs, and the terminal shows Adult ticket.

02:17 Then the last line runs, as it always does. Enjoy the trip.

02:23 Change the age to 70 and run the script again. Under 5 is false, and under 16 is false. Then 65 or older is true, so the senior block runs.

02:36 The else block doesn’t run, even though the if’s condition was false. In a chain, else runs only when every condition is false. Once one branch has run, the rest of the chain is skipped without another check.

02:50 The terminal shows Senior ticket, then wishes a good trip. Change the age to 3. This time, two conditions would be true on their own. Three is under 5, and three is also under 16.

03:05 Your turn: pause the video and predict which ticket the terminal shows.

03:15 Free entry. JavaScript checks from the top, and under 5 is the first true condition, so its block runs. The child check never gets asked. That’s why it only needs under 16: anyone under 5 has already been handled above it.

03:33 An age of 5 isn’t under 5, so the first check is false. Five is under 16, so the terminal shows Child ticket, and the senior check is skipped.

03:45 So here’s the rule. JavaScript checks the conditions from top to bottom. It runs the first branch whose condition is true, and it skips the rest. If none is true, the else block runs.

03:59 Nice work. With else and else if, every visitor at the desk gets exactly one ticket. In the next lesson, you’ll compare a chain with the other shapes a decision can take.

04:09 It’s called Separate ifs, else if, or a Nested if?

Become a Member to join the conversation.