Help on the switch statement?


#1

Hello :smile:

Hope your all ok ? :smile:

so I dont completely understand the switch statement :confused:

Any help would be awesome :smile:


#2

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

Could you be a bit more precise?
The syntax is

switch(value){
case ... :
...
break;
[...]
case ... :
...
break;
default:
...
}

where the values after case and between the : are compared to the value after (). The first matching one is chosen and default is kind of like the else that gathers the rest. break; let's you skip this because otherwise you would go on with the code below even this the value after case is not matched.


#3

Sorry I wasn't that clear aha,

What I meant is, Like basically how do they work, what does the switch statment do ? :smile:

I Know it explains it, but I cant understand it :confused:


#4

Maybe also have a look at this definition in the glossary:

https://www.codecademy.com/articles/glossary-javascript

Acts like a big if / else if / else chain. Checks a value against a list
 of cases, and executes the first case that is true. It goes on 
executing all other cases it finds after the first true case till it 
finds a breaking statement,after which it breaks out of the switch If it
 does not find any matching case , it executes the default case.

#5

Ah thanks a lot for explaining :smile: , I was just super confused by it aha , thanks again :smile: