JavaScript - Conditional Statement

A conditional statement is a set of commands that executes if a specified condition is true.

JavaScript supports two conditional statements: if...else and switch.

 

If Condition
Use of if statement to perform certain statements if a logical condition is true; use the optional else clause to perform other statements if the condition is false.

Syntax : 

if ( condition){
          statements1
}else{
          statements2
}
       

Example :

<html>
<body>
<script language=“javascript”>
var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("<b>Good morning</b>");
}
else
{
document.write("<b>Good day</b>");
}
</script>
</body>
</html>
 

In above example we get current browser date and time by inbuilt function Date () and getHours() that we see in near future.


In variable d we store current date for getting hours from that.

Variable time will store current time in hours, if a condition check for time is less than 10.

If time is less than 10 then “Good morning” message is printed otherwise control goes to else block and “Good day” message is displayed on web page.

 
Switch Statement

A switch statement allows a program to evaluate an expression and attempt to match the expression’s value to a case label.

If a match is found, the program executes the associated statement.

The program first looks for a label matching the value of expression and then executes the associated statement.

If no matching label is found, the program looks for the optional default statement, and if found, executes the associated statement.

If no default statement is found, the program continues execution at the statement following the end of switch.

The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch.

If break is omitted, the program continues execution at the next statement in the switch statement.

Syntax : 

switch ( expression)

{
    case label :
               statement;

               break;

    case label :
              statement;
              break;

     default:

             statement;
}

Example :   

<html>
<body>
<script language=“javascript”>
var d = new Date();
theDay=d.getDay();

switch (theDay)
{
case 5:
     document.write("<b>Finally Friday</b>");
     break;

case 6:
     document.write("<b>Super Saturday</b>");
     break;
case 0:
     document.write("<b>Sleepy Sunday</b>");
     break;

default:
     document.write("<b>Good Weekend </b>");
}
</script>
</body>
</html>

In above example getDay() gets days number from current date.

Above example will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc.

The code will store day value in variable theDay and depending on this value message inside case is displayed on web page.

D.getDay will always give some numerical value.

Numerical value for Sunday is 0, and for Saturday it is 6. If theDay value doesn’t satisfy given case statement then default case is executed.