Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen The `else if` Clause | Section
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
JavaScript Essentials for Beginners - 1768407374405

bookThe `else if` Clause

In addition to the else clause, conditional statements support an else if clause, which can be used to define alternative conditions if the initial if condition is false.

The general syntax is as follows:

if(expression) {
  // Code … (executed if the expression is true)
} else if(expression) {
  // Fallback Code … 
  //(executed if the previous condition is false, and this one is true)
}

As seen in the general syntax, the else if clause takes a boolean expression, which is evaluated when the condition before it turns out to be false.

We can chain multiple else if clauses to form an if-else if chain:

if(expression) {
  // … (executed if the first condition is true)
} else if(expression) {
  // … (executed if the first condition is false and this is true)
} else if(expression) {
  // … (executed if previous conditions are false and this is true)
} else {
  // … (executed if all previous conditions are false)
}

As shown in the code above, we can optionally add the else clause at the end. This block is executed only when all the previous conditions evaluate to false.

The following example demonstrates the usage of this syntax:

123456789
let number = 50; if (number < 20) { console.log("The number is less than 20."); } else if (number === 20) { console.log("The number is exactly 20."); } else { console.log("The number is greater than 20."); }
copy
question mark

What is the purpose of the else if clause in an if-else statement?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 39

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookThe `else if` Clause

Swipe um das Menü anzuzeigen

In addition to the else clause, conditional statements support an else if clause, which can be used to define alternative conditions if the initial if condition is false.

The general syntax is as follows:

if(expression) {
  // Code … (executed if the expression is true)
} else if(expression) {
  // Fallback Code … 
  //(executed if the previous condition is false, and this one is true)
}

As seen in the general syntax, the else if clause takes a boolean expression, which is evaluated when the condition before it turns out to be false.

We can chain multiple else if clauses to form an if-else if chain:

if(expression) {
  // … (executed if the first condition is true)
} else if(expression) {
  // … (executed if the first condition is false and this is true)
} else if(expression) {
  // … (executed if previous conditions are false and this is true)
} else {
  // … (executed if all previous conditions are false)
}

As shown in the code above, we can optionally add the else clause at the end. This block is executed only when all the previous conditions evaluate to false.

The following example demonstrates the usage of this syntax:

123456789
let number = 50; if (number < 20) { console.log("The number is less than 20."); } else if (number === 20) { console.log("The number is exactly 20."); } else { console.log("The number is greater than 20."); }
copy
question mark

What is the purpose of the else if clause in an if-else statement?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 39
some-alt