Skip to main content

JavaScript else if Statement (Live Playground)

In this tutorial, we will explore the else if statement, and learn how to use it in our code.

else if Statement

The else if statement is a conditional statement which used to make decisions in your code based on specific conditions.

The else if statement is used to test multiple conditions in a single structure. It is placed between an if and an else statement and is executed if the preceding condition is false and the current condition is true.

Example:

const a = 5;

if (a > 10) {
console.log('a is greater than 10');
} else if (a > 5) {
console.log('a is greater than 5');
} else {
console.log('a is not greater than 5 or 10');
}

In the example above, the output is 'a is not greater than 5 or 10' because both conditions a > 10 and a > 5 are false.

Live Playground, Try it Yourself

Conclusion

Understanding and using else if statement is crucial for controlling the flow of your JavaScript programs based on specific conditions.