Break out of a switch block to ensure that only one case is executed: - Javascript Statement

Javascript examples for Statement:switch

Description

Break out of a switch block to ensure that only one case is executed:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from w w  w  .j  av a 2s . c  om*/
    var day;
    switch (new Date().getDay()) {
        case 0:
            day = "Sunday";
            break;
        case 1:
            day = "Monday";
            break;
        case 2:
            day = "Tuesday";
            break;
        case 3:
            day = "Wednesday";
            break;
        case 4:
            day = "Thursday";
            break;
        case 5:
            day = "Friday";
            break;
        case  6:
            day = "Saturday";
            break;
    }
    document.getElementById("demo").innerHTML = "Today is " + day;
}
</script>

</body>
</html>

Related Tutorials