Javascript switch statement tutorial

Javascript switch statement is used to execute a block of statement based on the switch expression value. It is like if else if statement.

Syntax:

switch(expression){ 
case value1: 
//Javascript block of statements 
break;   
case value2: 
//Javascript block of statements 
break;   
...   
default: 
//Javascript block of statements 
break;   
}

JavaScript Control Statements Example:

<html>
<head>
<script>  
var num=2;  
if(num==1){  
document.write("JavaScript Statement 1");  
}  
else if(num==2){  
document.write("JavaScript Statement 2");  
}  
else if(num==3){  
document.write("JavaScript Statement 3");  
}  
else{  
document.write("JavaScript Statement n");  
}  
</script>  
</head>
<body>
</body>
</html>

Try it:

JS Bin on jsbin.com