0
Lesson "Conditionals and Loops The switch Statement" 4/4
Hi , in Lesson "Conditionals and Loops The switch Statement" 4/4, I don't understand this sentence :" The default block can be omitted, if there is no need to handle the case when no match is found. " ...so nothing appears at the screen ? Is that correct ?
2 Antworten
+ 3
The sentence means that you may avoid the default clause, if there is no need of it. For example,
var i = 2;
switch(i) {
case 1:
console.log("one");
break;
case 2:
console.log("two");
break;
default:
console.log("none");
}
Here, you know that i is 2, so the second case will be executed. Hence there is no need of using default, as it will never be executed with that value.
+ 1
IT IS SIMILAR TO ELSE-IF STATEMENT.
IN SWITCH STATEMENT YOU ARE PASSING A VALUE AND SEE IF ANY CASE MATCHES THAT VALUE IF YES YOU EXECUTE THAT BLOCK AND BREAK OUT FROM SWITCH STATEMENT.
IF ANY CASE DOESNT MATCH WITH VALUE DEFAULT CASE IS EXECUTED WHICH IS SIMILAR TO ELSE IN ELSE-IF.