How to using if...else Statement in JavaScript

JavaScript - if...else Statement


While creating a program, their strength is a situation when you have to get one out of a given arrangement of ways. In such events, you have to use conditional explanations that enable your program to settle on rectify choices and perform the right activities. 

JavaScript underpins restrictive explanations that are utilized to perform distinctive activities in light of various conditions. Here we will clarify the if..else articulation.


JavaScript supports the following forms of if..else statement −

  • if statement
  • if...else statement
  • if...else if... statement.

if statement


The if the explanation is the key control articulation that enables JavaScript to settle on choices and execute proclamations restrictively.



Syntax

The syntax for a basic if statement is as follows −

if (expression){
   Statement(s) to be executed if expression is true
}
Here a JavaScript articulation is assessed. On the off chance that the subsequent esteem is valid, the given statement(s) are executed. In the event that the articulation is false, at that point, no announcement would be not executed. The majority of the circumstances, you will utilize correlation administrators while deciding.


Example

Try the following example to understand how the if statement works.

<html>
   <body>
      
      <script type="text/javascript">
         <!--
            var age = 20;
         
            if( age > 18 ){
               document.write("<b>Qualifies for driving</b>");
            }
         //-->
      </script>
      
      <p>Set the variable to different value and then try...</p>
   </body>
</html>

if...else statement:

The 'if...else' statement is the next form of control statement that allows JavaScript to execute statements in a more controlled way.

Syntax

if (expression){
   Statement(s) to be executed if expression is true
}
else{
   Statement(s) to be executed if expression is false
}
Here JavaScript articulation is assessed. In the event that the subsequent esteem is valid, the given statement(s) in the 'if' square, are executed. On the off chance that the articulation is false, at that point the given statement(s) in the else square are executed.

Example

Try the following code to learn how to implement an if-else statement in JavaScript.
<html>
   <body>
   
      <script type="text/javascript">
         <!--
            var age = 15;
         
            if( age > 18 ){
               document.write("<b>Qualifies for driving</b>");
            }
            
            else{
               document.write("<b>Does not qualify for driving</b>");
            }
         //-->
      </script>
      
      <p>Set the variable to different value and then try...</p>
   </body>
</html>

if...else if... statement

Example

Try the following code to learn how to implement an if-else-if statement in JavaScript.
<html>
   <body>
   
      <script type="text/javascript">
         <!--
            var book = "maths";
            if( book == "history" ){
               document.write("<b>History Book</b>");
            }
         
            else if( book == "maths" ){
               document.write("<b>Maths Book</b>");
            }
         
            else if( book == "economics" ){
               document.write("<b>Economics Book</b>");
            }
         
            else{
               document.write("<b>Unknown Book</b>");
            }
         //-->
      </script>
      
      <p>Set the variable to different value and then try...</p>
   </body>
<html>

No comments