Switch or if, else if, else?

+3 Tom Haynes · December 23, 2015
Why wouldn't you use if, else if, else instead of switch? Doesn't this have the same functionality?

Post a Reply


Oldest  Newest  Rating
0 Linguist Llama · December 23, 2015
Did you try looking for an answer to this question before asking it?

If so, perhaps you could have put a little bit more effort into thinking about keywords.

I was able to find a nice discussion on this topic by using the following keywords: switch vs if else javascript (http://lmgtfy.com/?q=switch+vs+if+else+javascript&l=1)

You can help save the internet (and your fingers, think about how I used less keystrokes in my keywords) by making an effort to reduce the amount of redundant information it contains. GOOGLE before you ask.
0 Scott Walker · December 26, 2015
The functionality is the same, it's just a conditional statement that checks if something is true and then if it is runs something.

The only real difference is that switch statements are slightly cleaner in terms of being 'visually pleasing' rather than a bunch of if statements with a shed load of curly braces etc
  • 1



A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.