When there is more than one condition to be checked for, you can use the if..else..if chain.
Syntax:
if (condition 1) { // code to be executed when condition 1 holds true } else if (condition 2) { // code to be executed when condition 2 holds true } else if (condition 3) { // code to be executed when condition 3 holds true } else if (condition n) { // code to be executed when condition n holds true } else { // code to be executed when neither condition holds true }
Note that if either of the conditions holds true, the code corresponding to that condition is executed and the control breaks out of the conditional statements, meaning that the subsequent conditions are not checked for. In case none of the conditions holds true, the code written inside the else-block is executed.
Example:
var x = 0; if (x === 0) { console.log("Neither positive nor negative!"); } else if (x > 0) { console.log("Positive!"); } else { console.log("Negative!"); }
Output:
"Neither positive nor negative!"