else if in JavaScript? | Sololearn: Learn to code for FREE!
+ 1

else if in JavaScript?

What is the purpose of adding else if in JS if you have a better option (switch)?

17th Mar 2018, 8:27 AM
Syed Riyaz Uddin
Syed Riyaz Uddin - avatar
1 ответ
+ 5
Use switch instead of if when: 1- You are comparing multiple possible conditions of an expression and the expression itself is non-trivial. 2- You have multiple values that may require the same code. 3- You have some values that will require essentially all of another value's execution, plus only a few statements. Use if instead of switch when: 1- You want to test for the truthiness of an expression. 2-You only have a single affirmative test. 3-You need to evaluate different expressions for each branch.
17th Mar 2018, 8:34 AM
Baraa AB
Baraa AB - avatar