JavaScript if else Statement – Tutorialpath

Welcome to all our readers on our website at www.tutorialpath.com. As you all know that here on our website we will provide you lots of information related to the latest and newly introduced technology which helps you to get updated with the latest tech. Here you will get all the useful information which is required in this tech world. As you know technology is one of the trending parts of this world. So, here again, we came up with the latest and new tech information which is related to the process of creating JavaScript if-else Statement. With the help of this article, you will get to know the complete process of how to create JavaScript if-else Statement in a step by step manner. So, simply have a look at this article and grab all the useful information which is going to be very helpful for you.

JavaScript Conditional Statements: IF, Else, Else IF

 

How to use Conditional Statements

JavaScript supports conditional statements that are used to perform various tasks based on different conditions. If a condition is true, you can take one action and if the condition is false, you can take another action.

Different Types of Conditional Statements

There are mainly three types of conditional statements in JavaScript.

  1. If statement
  2. If…Else statement
  3. If…Else If…Else statement

If Statement:

If the statement is the simplest decision-making statement. It is used to decide whether a certain statement or statement will be executed, i.e. if a block of statements is executed if a certain condition is true.

Syntax:

              If (condition)

                {  

                 // Statements to execute if

              // condition is true 

              }  

Example:

             function testNum(a) {

                if (a > 0) {

                         return “Good”;

               } else {

                        return “NOT Good”;

                        }

              }

              console.log(testNum(-5));

               // expected output: “NOT Good”

Output:

NOT Good

Note:- The JavaScript expression is evaluated here. If the resulting value is true, the given statement is executed. If the expression is false, no statement will be executed. Most of the time, you will use comparison operators when deciding.

if…else statement

The ‘if … else’ statement is the next form of control statement, which allows JavaScript to execute statements in a more controlled way. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.

Syntax:

                if (condition)

                  {

                       lines of code to be executed if the condition is true

                 }

                      else

                              {

                                 lines of code to be executed if the condition is false

                              }

Example:

<html>

   <body>

      <script type = “text/javascript”>

         <!– var age = 15;

                    if( age > 18 ){

                    document.write(“<b> driving</b>”);

                   }

                    else{

               document.write(“<b>Does not driving</b>”);

            }

         //–>

      </script>

           <p>Set the variable to different value and then try.</p>

   </body>

</html>

Output:

Does not driving

Set the variable to different value and then try.

If…else if statement

A user can decide between several options. If statements are executed from top to bottom. As soon as if one of the controlling conditions is true, the statement related to it is executed, and the rest of the ladder is bypassed. If none of the conditions is true, the last statement will be executed.

Syntax:

              if(condition expression)

                  {

                    //Execute this code block

                  }

                        else if(condition expression) {

                       //Execute this code block

                  }

Example:

<html>

<body>  

      <script type = “text/javascript”>

         <!–

            var book = “science”;

            if( book == “history” ) {

               document.write(“<b>History Book</b>”);

            } else if( book == “science” ) {

               document.write(“<b>Science 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>

Output:

Science Book

Set the variable to different value and then try…

Conclusion

So, hopefully we have shared all the basic steps that help you JavaScript Conditional Statement IF, ELSE, IF-ELSE.  In this tutorial, you have to learn basic several ways for JavaScript .

Leave a Reply

Your email address will not be published. Required fields are marked *