Skip to main content

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

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.







let salary = 50000;

if(salary <= 30000)
    console.log('Salary Credited')
else if(salary == 50000) 
    console.log('More than 30000')
    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.


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

Submitted by arilio666 on October 1, 2021

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.


At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.