Home >>Javascript Tutorial >JavaScript If-else

JavaScript If-else

JavaScript If-else

The if-else statement in JavaScript is generally used to execute the code to determine whether the condition is true or false. In JavaScript, there are three forms of if-statement.

  • If Statement
  • If else statement
  • If else if statement

1.If statement in JavaScript

If statement computes the content only and only if the expression is true. Here is the syntax of the if statement:

Syntax

<script>
if(expression)
{  
//content that is to be evaluated  
}  
</script>

Here is an example of If statement in JavaScript:

Example

<script>  
var x=22;  
if(x>11)
{  
document.write("value of x is bigger than 11");  
}  
</script>  
Output :value of x is bigger than 11.

2. If-else statement in JavaScript

If-else statement computes the content whether the condition is true or false. It means if condition is true then print some statement otherwise print some different statement.
Here is the syntax of if-else statement:

<script>
if(expression)
{  
//content that is to be evaluated if condition is true  
}  
else
{  
//content that is to be evaluated if condition is false  
}  
</script>

Here is an example of the if-else statement to find out the whether the number is even or odd:

Example

<script>  
var x=10;  
if(x%2==0)
{  
document.write("x is even number");  
}  
else
{  
document.write("x is odd number");  
}  
</script>  
Output : x is even number

Here is 2nd example of the if-else statement to find out the whether the number is negative or positive:

Example

<script>  
var x=10;  
if(x>=0)
{  
document.write("x is positive number");  
}  
else
{  
document.write("x is negative number");  
}  
</script>  
Output : x is positive number

Here is 3rd example of the if-else statement to find out Profit and Loss:

Example

<script>  
var sp=100;
var cp=50;  
if(sp>cp)
{
var profit=sp-cp;  
document.write("Profit="+profit);  
}  
else
{  
var loss=cp-sp;  
document.write("Loss="+loss); 
}  
</script>  
Output : Profit=50

3. If-else-if statement in JavaScript

If-else-if statement computes the content only if expression is true from various.

When we want to use condtion more than one times then we can use Nested if-else.

syntax :

<script>
if(expression1)
{  
//content that is to be evaluated if expression1 is true  
}  
else if(expression2)
{  
//content that is to be evaluated if expression2 is true  
}  
else if(expression3)
{  
//content that is to be evaluated if expression3 is true  
}  
else
{  
//content that is to be evaluated if no expression is true  
}  
</script>

Here is an example of else-if-else statement in JavaScript;

Example 1

<script>
var x=10;  
if(x==5){  
document.write("x is equal to 5");  
}  
else if(x==10){  
document.write("x is equal to 10");  
}  
else if(x==15){  
document.write("x is equal to 15");  
}  
else{  
document.write("x is not equal to 5, 10 or 15");  
}  
</script>
Output : x is equal to 10

Example 2(Check days)

<script>
var day=3;  
if(day==1)
{  
document.write("Today is monday");  
}  
else if(day==2)
{  
document.write("Today is tuesday");  
}  
else if(day==3)
{  
document.write("Today is Wednesday");  
}  
else
{  
document.write("Wrong Choice");  
}  
</script>
Output : Today is Wednesday

No Sidebar ads