+ 7
Can someone explain it to me
Okay, so I am fairly new to JS, I don't really understand the "switch" nor "else if" things. I was wondering if anyone could help me by any chance and explain it to me? I am autistic so I apologize in advance for the stress of explaining it.
3 Respuestas
+ 6
i think switch and if-else have same result.
but you can use switch instead of if-else in order to speeding up you in writing code so you can use switch to define what's condition if user input write something or do something.
Example:
//use if
if(animals == 1){
alert("cat")
} else if (animals === 2) {
alert("crocodile");
} else if (animals === 3) {
alert ("dog");
} else {
alert ("there's no animal");
}
//use switch
switch (animals) {
case 1:
alert("cat");
break;
case 2:
alert("crocodile ");
break;
case 3:
alert("dog");
break;
default :
alert("there's no animal ");
}
/*you have to write break in order to stop the code after executed*/
+ 4
So it's just another way of the if statement ?
0
i dont think we can write more codes inside the Switch,Case condition though switch is fast..
like,
else{
//loops, functions
}