JavaScript: if...else if... statement

Profile picture for user arilio666

The else if is used to execute the statement block when the first if body fails or becomes false the condition gets triggered up to execute the condition.

Syntax:

if(){

}elseif(){

}else{

}

Example:

let salary = 50000;

if(salary <= 30000)
{
    console.log('Salary Credited')
}
else if(salary == 50000) 
{
    console.log('More than 30000')
}
else
{
    console.log('Out of block')
}

Output: More than 30000

Here I added a new else if body to convey that the salary is higher than 30k. S,o at first it hecks the if part and concludes that it didn't satisfy the need. It then goes to the following else if part and when it satisfies it goes inside its block and executes the code ignoring the else part. Remaining two two blocks do not satisfy the need, hence not executed.

Verdict:

If you wanna write two conditions then else if is the thing for you to validate with two conditions.