+ 1
else if in JavaScript?
What is the purpose of adding else if in JS if you have a better option (switch)?
1 Resposta
+ 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.