Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære The `else if` Clause | Conditional Statements
Introduction to JavaScript
course content

Kursinnhold

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
The `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:

js

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:

js

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 10

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

course content

Kursinnhold

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
The `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:

js

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:

js

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 10
Vi beklager at noe gikk galt. Hva skjedde?
some-alt