Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
  1. JavaScript: Basics
    9Topics
    |
    10 Quizzes
  2. JavaScript: Variables
    9Topics
    |
    10 Quizzes
  3. JavaScript: Conditional Statements
    10Topics
    |
    11 Quizzes
  4. JavaScript: Functions
    10Topics
    |
    11 Quizzes
  5. JavaScript: Scope
    6Topics
    |
    7 Quizzes
  6. JavaScript: Arrays
    11Topics
    |
    12 Quizzes
  7. JavaScript: Loops
    8Topics
    |
    9 Quizzes
  8. JavaScript: High Order Functions
    3Topics
    |
    4 Quizzes
  9. JavaScript: Iterators
    6Topics
    |
    6 Quizzes
Lesson 3, Topic 9
In Progress

JavaScript: Else If Statements

Yasin Cakal 2 Nov 2021
Lesson Progress
0% Complete

An else if statement can be used to add more conditions to our if…else. If you use the else if statement, you can have more than two outcomes. You can add as many additional if statements as you want to create more complex conditionals!

The else if statement is always followed by the if statement and preceded by the else statement. A condition is passed to the else if statement too. Consider the following syntax:

let day = 'Saturday';
 
if (day === 'Sunday') {
  console.log('Yay its weekend!');
} else if (day === 'Saturday') {
  console.log('Let the party begin.');
} else if (day === 'Monday') {
  console.log('Oh! Whole week to roll!');
} else {
  console.log('Waiting for the weekend!');
}

//Output:Let the party begin.

You can have multiple possible outcomes with the else if statements. If/else statements are read from top to bottom, so the block that gets executed is the first condition that evaluates to true from top to bottom.

Because day==='Sunday' evaluates to false and day==='Saturday' evaluates to true in the above example, the code inside the first else if statement is executed. The remaining conditions are not assessed. The code in the else statement would have been executed if none of the conditions were true.

Responses

Leave a Reply

%d bloggers like this: