JAVASCRIPT CASES


In javascript we use cases to perform different type of actions depending on user input or data evaluated from set of functions.

    Syntax:

    switch(condition){
    case number:statement;
    break;

    case number:statement;
    break;

    default:statement;
    break;
    }

BREAK in javascript:

We use break statement in javascript to jump out of one particular case on function depending on requirement.

Example:

Explaination:

<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="output">
</p>
<script>
switch( new Date().getDay()){
case 0:day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day="Teusday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
}
document.getElementById
("output").innerHTML = "Enjoy" + day;
</script>
</body>
</html>

If-else condition in javascript

    Syntax:

    if(condition){
    coding
    }
    else(condition){
    coding
    }
If-else condition works when condition given in if is satisfied.If If condition is not satisfied then else function works.If function can work without else but else can not work without If.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="ot">
</p>
<script>
if(new Date().getHours()<24){
document.getElementById
("ot").innerHTML="How was your day?";
}
else{
document.getElementById
("ot").innerHTML="Enjoy your day!";
}
</script>
</body>
</html>

Explaination: