JavaScript else
Statement (Live Playground)
In this tutorial, we will explore the else
statement, and learn how to use it in our code.
else
Statement
The else
statement is a conditional statement which used to make decisions in your code based on specific conditions.
The else
statement is used to execute a block of code if the condition in the preceding if
statement is false
.
Example:
const a = 4;
if (a > 5) {
console.log('a is greater than 5');
} else {
console.log('a is not greater than 5');
}
In the example above, the output is 'a is not greater than 5' because the condition a > 5
is false
.
Live Playground, Try it Yourself
Conclusion
Understanding and using else
statement is crucial for controlling the flow of your JavaScript programs based on specific conditions.