programminghead Logo

JavaScript if else else if Conditions

Introduction :

Javascript If else conditional statement allows us to add a particular Condition to a particular Data. So Data be executed only when the defined Condition is true. For this type of work, we have Different Conditional statements in JavaScript, Like :
JavaScript If Condition,
JavaScript If Else Condition and
JavaScript Else If Condition

javascript-if-else-else-if-conditions-Image

JavaScript if condition:

In JavaScript, if Conditional statement performs tasks according to given condition.
if the condition is true:
If the given condition is true (Returns true) then our Programme/Browser will Execute/run the data declared inside the if condition.
if the condition is false:
If the given condition returns false, then our programme/Browser will Ignore the data Declared/Present inside that if Condition.

JavaScript if Conditions Syntax:

To include JavaScript if Condition, We have to write if flowwed by two round brackets and two semi-columns.
Where round brackets will hold the Condition and semi-columns will hold the statements.

JavaScript IF Conditions Syntax:


   if(condition)
   {
      Statements...
   }

JavaScript if Conditions Example:


<html>
 <body>

   <script type="text/javascript">

     var a=10;
     
     if(a==10)
     {
       document.write("Condition is True");
     }

   </script>
 </body>
</html>

Result

JavaScript if else Condition

JavaScript if else condition is very similar to if condition. Where in if condition the programme/browser will only execute the condition's data/statement if the given condition is true, Otherwise browser will ignore those data/statements. But in if else condition browser will first check the if condition.
if the condition Return true:
browser will execute the given data/statement inside.
if the condition Return false:
then the browser will go for else statement and execute the else's data/statement.

JavaScript if else Conditions Syntax:


   if(condition)
   {
      if Statements...
   }
   else
   {
      else Statements...
   }

JavaScript if else Conditions Example:


<html>
 <body>

   <script type="text/javascript">

     var a=0;
     
     if(a==10)
     {
       document.write("A is equals to 10");
     }
     else
     {
       document.write("A is not equals to 10");
     }

   </script>
 </body>
</html>

Result


Now for the single condition we can use JavaScript's if Condition. By using if else condition we can set a Statment if the if condition returns FALSE then else condition will execute. But while using JavaScript, there are fair chances where we have to use more then 2 conditions which can't be done by if and if else conditions. So for multiple conditions, we have else if condition.

JavaScript else if Condition :

JavaScripts else if condition allows us to use multiple conditions in JavaScript. Where we can add multiple conditions to the programme using multiple else if statements.

JavaScript else if Conditions Syntax :

First, we have to write the whole if else syntax as it is. and between if statement and the else statement we have to add else if statement.
So browser will check the primary (if condition) first and if the primary condition return false then the browser will check the else if condition. The process will be the same.

JavaScript else if Conditions Syntax:


   if(first condition)
   {
      if Statements...
   }
   else if(second condition)
   {
      else Statements...
   }

JavaScript else if Conditions Example:


<html>
 <body>

   <button onclick="wish()">Wish - click me</button>

   <script type="text/javascript">
   
   function wish(){
   
     var time = new Date().getHours();
     if(time < 10) {
       document.write("Good morning");
     } 
     else if(time < 20) {
       document.write("Good day");
     } 
     else {
       document.write("Good evening");
     }
     
    }

   </script>
 </body>
</html>

Result

Comments

Write to Us

Captcha