CodesJava

Easy learning with example program codes

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;   
}

:

<html>
<head>
<script>  
var today;
switch (new Date().getDay()) {
    case 0:
        today = "Sunday";
        break;
    case 1:
        today = "Monday";
        break;
    case 2:
        today = "Tuesday";
        break;
    case 3:
        today = "Wednesday";
        break;
    case 4:
        today = "Thursday";
        break;
    case 5:
        today = "Friday";
        break;
    case  6:
        today = "Saturday";
}
document.write("Today is " + today);
</script>  
</head>
<body>
</body>
</html>

Try it:

JS Bin on jsbin.com
 



Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap