Javascript Control Statements

A Javascript control statement is used to control the flow of the program based on the specified condition.

Javascript control statements:

1. If Statement
2. If else statement
3. if else if statement

JavaScript If Statement:

If the statement is used to execute a block of statements if the specified condition is true.

Syntax:

if(condition){
//Block of JavaScript statements.
}

JavaScript If Else Statement:

If else statement is used to execute either of two blocks of statements depends upon the condition. If the condition is true then if block will execute otherwise else block will execute.

Syntax:

if(condition){
//Block of JavaScript statements1.
}else{
//Block of JavaScript statements2.
}

JavaScript If Else If Statement:

If else statement is used to execute one block of statements from many depending upon the condition. If condition1 is true then a block of statements1 will be executed, else if condition2 is true block of statements2 is executed, and so on. If no condition is true, then else block of statements will be executed.

Syntax:

if(condition1){
//Block of JavaScript statements1.
}else if(condition2){
//Block of JavaScript statements2.
} . . . else if(conditionn){
//Block of JavaScript statementsn.
}else{
//Block of JavaScript statements.
}

JavaScript Control Statements Example:

<html>
<head>
<script>
var num=2;
if(num==1){
document.write("JavaScript Statement 1");
}
else if(num==2){
document.write("JavaScript Statement 2");
}
else if(num==3){
document.write("JavaScript Statement 3");
}
else{
document.write("JavaScript Statement n");
}
</script>
</head>
<body>
</body>
</html>

Try it:

JS Bin on jsbin.com