The switch statement part 4

The default Keyword



The default keyword specifies the code to run if there is no case match.
var color ="yellow";
switch(color) {
case "blue":
document.write("This is blue.");
break;
case "red":
document.write("This is red.");
break;
case "green":
document.write("This is green.");
break;
case "orange":
document.write("This is orange.");
break;
default:
document.write("Color not found.");
}

//Outputs "Color not found."
Try It Yourself

The default block can be omitted, if there is no need to handle the case when no match is found.
The "default" statement is used ...
To finish the "case" statement
Because it is obligatory
When no match is found

Comments

Popular posts from this blog

Introducing object part 3

Comparison Operators part 2

Module 2