Use the if else if statement if you want to check 2 conditions in JavaScript. Use if
to specify a block of code to be executed, if a specified condition is true.
And Use else if
to specify a new condition to test, if the first condition is false
if (condition) {
// statement
} else if(condition){
// statement
}else{
// statement
}
Example if else if JavaScript
A simple example code uses the else if
statement to specify a new condition if the first condition is false.
<!DOCTYPE html>
<html>
<body>
<script>
var greeting= "Hello"
var time = 15;
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
console.log(greeting)
</script>
</body>
</html>
Output:

Multiple if...else
statements can be nested to create an else if
clause.
if (condition1)
statement1
else if (condition2)
statement2
else if (condition3)
statement3
...
else
statementN
Do comment if you have any doubts or suggestions on this JS if else topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.