Skip to the content

else and else if

JavaScript · Unit 2: Conditionals

else is the other path: it runs when the condition of the if is false.

It takes no condition, and its block also goes between braces.

let age = 15;
if (age >= 18) {
  console.log("Adult");
} else {
  console.log("Minor");
}

Prints

Minor

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Complete the code

    Fill in the blank so it says whether the number is even or odd.

  3. 3. Multiple choice

    When does the block of the else run?

  4. 4. Predict the output

    What does this code print?

  5. 5. Predict the output

    What does this code print?

  6. 6. Complete the code

    Fill in the blank so it prints a single message.

  7. 7. Predict the output

    What does this code print?

  8. 8. Find the bug

    It should greet you according to the hour, and it is 21. Which line has the error?

  9. 9. Put the lines in order

    Put the lines in order to sort the age: child under 12, teen under 18 and adult at 18 or older.

  10. 10. Predict the output

    What does this code print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →