JavaScript Tutorial

Statement in JavaScript
Previous Home Next

A statement is the smallest standalone element in programming language which expresses some action to be carried out. A statement may have internal components (e.g., expressions). A program written in such a language is formed by a sequence of one or more statements

JavaScript supports following statement :

  • if statement : use this statement if you want to execute a set of code when a condition is true.
  • if...else statement : use this statement if you want to select one of two sets of lines to execute.
  • if...else if... statement : is the one level advance form of control statement.
  • switch statement : use this statement if you want to select one of many sets of lines to execute.

if statement :

You can use the if statement to check the if a condition is true or false.

Syntax

if (condition)
{
 javascript code to be executed 
}

Example :

<html>
<body>
<script>  
var a=20;  
if(a<5)
{  
document.write("Condition True");  
} 
document.write("Condition False");
</script>  
</body>
</html>

 

Output :

Condition False

if...else statement :

The If else..Statement is a way to make decisions based on a variable or some other type of data. For example, you might have a script that checks if condition or value is true or false,

Syntax

if (condition)
{
   javascript code to be executed if condition is true
}
else
{
   javascript code  to be executed if condition is false
}

Example :

<html>
<body>
<script>  
var a=20;
var b=40;  
if(a<b){  
document.write("true"); 
}  
else{  
document.write("false");  
}  
</script>
</body>
</html>

Output :

true

if...else if... statement :

The if else if... statement is the one level advance form of control statement that allows to make correct decision out of several conditions

Syntax

if (condition1)
{  
javascript code to be executed if condition1 is true  
}  
else if(condition2)

javascript code to be executed if condition2 is true 
 }  
else if(condition3)
{  
javascript code to be executed if condition3 is true 
else{  
javascript code to be executed  if no condition is true  
}  

Example :

<html>
<head> 
<script>  
    var a=20;
    var b=30;  
    if(a<=b)
{  
    document.write("Good morning");  
    }  
    else if(a>=b){ 
    document.write("Good day");  
    }  
    else if (a>=b){  
    document.write("Good night");  
    }  
    else{  
    document.write("Good evening");  
    }  
    </script> 
<head>
<html>

Output :

Good morning

Switch statement :

When we know various or more possibilities comes in a expression then switch statement is an efficient way to do a certain operation on given code

Syntax

switch (expression)
{
case value:
condition;
break;

case value:
condition;
break;

default:
condition;
}

Example :

<html>
<head>
<script>
var day=5;

switch (day)
{
case 0:
document.write ("Today is Sunday");
break;
case 1:
document.write ("Today is Monday");
break;

case 2:
document.write ("Today is Tuesday");
break;

case 3:
document.write ("Today is Wednesday");
break;

case 4:
document.write ("Today is Thursday");
break;

case 5:
document.write ("Today is Friday");
break;

case 6:
document.write ("Today is Saturday");
break;

default:
document.write ("any days");
break;
}
</script>
</head>
</html>

Output :

Today is Friday
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: