JavaScript if Statements

JavaScript If Statements are also called conditional statements. The statements are used when execution of a program or script depends on the value of a test condition.  This comes from the fact that programs cannot be necessarily sequential. There can be different paths in a program depending upon value of certain variables of expression. This is used to implement decision taking in a program or a script.

Types of JavaScript If Statements

  • If Statement
  • If- Else Statement
  • Switch Statement

In all these conditional statements, test condition is specified using the conditional and logical operators.

If Statement

The if statement executes a block of statements when the test condition becomes true. If the test condition is false, the statements after the if block are executed.

Syntax

if (condition)
{
	If block statements;
}
Remaining statements ;
Example
<SCRIPT LANGUAGE="javascript">
    empName='Amy';
    if (empName=='Amy')
    {
        document.write( "Hello " +empName+"! Please come<BR>");
    }
    document.write("No one else is Allowed except "+empName);
</SCRIPT>

If…else Statement

The if…else statement executes if block of the statement when test condition returns true. If the test condition results in false, the statements included in the else part are executed.

Syntax

if (test condition)
{
	if block statements;
}
else
{
	else block statements;
}
Remaining statements ;

Example

<SCRIPT LANGUAGE="javascript">
    empName='Amy';
    if (empName!='Amy')
    {
        document.write( "<BR><BR>Hello " +empName+"! Please come<BR>");
    }
    else
    {
        document.write("<BR><BR>No one else is Allowed except "+empName);
    }
    document.write("<BR>Program with start shortly<BR><BR>");
</SCRIPT>

You can nest if and if..else statements to add multiple condition in your JavaScript.

Switch-case statement

The switch case statement is another conditional statement in JavaScript. It gives you an advantage if you need to allow choosing one among multiple options. It is a better statement in comparison to nested if…else statement.

The switch statement starts with switch keyword. Switch  keyword is followed by the variable or expression in parenthesis. This expression or variable is compared against values given in the case statements of switch statement.  

When the value of expression given in parenthesis after switch keyword matches a case label, the statements associated with the chosen case statement will be executed. After each case statement block break statement can be given. It stops comparison of expression with remaining case labels.

If break statement is not given in each case block, the comparison of switch expression and case labels will continue even after a match has happened earlier.

When the switch variable or expression does not match any of the case labels, the statements of the default label are executed. Default label is not mandatory.

Syntax

switch( variable/expression)
{
case value1:
	case 1 statements
	break;
case value2:
	case 2 statements
	break;
:
Default: Default statements;
}

Example

<SCRIPT LANGUAGE="javascript">
dd='Sunday';
switch(dd)
{
case 'Monday':
	document.write("Week just started! long to go<BR>");
	break;
case 'Tueday':
	document.write( "Just overcame Monday Blues");
	break;
case 'Wednesday':
	document.write( "Half week past");
	break;
case 'Thursday':
	document.write( "Just keep patience for another day");
	break;
case 'Friday':
	document.write( "Thank God its Friday");
	break;
case 'Saturday':
	document.write( "Where's the party tonight");
	break;
default: 
	document.write( "Can't we have two sundays each week");
	break;
}
</SCRIPT>

So, you can see that he JavaScript If Statements give you flexibility of defining the flow of a script with values of variables of expressions.