How To Write IF-ELSE Statement In JavaScript
  Home
 

JavaScript If statement allows doing comparison and it does execute different type of decisions depending on the match. The JavaScript IF statement syntax is:

if (<your_condition>)
  {
     <execute this code if the condition is true>;
   }

The first example is done using condition “1”==”1″ and since this is always true the output text is showing “The IF statement is true“. We did include the HTML paragraph tags <p> and </p> so the text would look better on your web page.

<html>
  <body>
    <h2>My JavaScript If Statement</h2>

    <script type="text/javascript">
     if ("1"=="1") 
     {
      document.write("<p>The IF statement is true</p>");
      }
     </script>

    </body>
</html>

online tech support online computer help computer technician computer problems computer javascript classes javascript if javascript if else javascript else if javascript if else in javascript if in javascript javascript tutorial design learn web design javascript courses online javascript online course javascript classes javascript tutorial javascript

You can try this JavaScript If example on this link.

The ELSE syntax is almost the same as the IF with only one exception there is the else block added to the end.

if (<your_condition>)
  {
    <execute this code if the condition is true>;
   }
else
  {
    <otherwise execute this code>;
   }

This second example will show text of the else block and the reason is in the if condition there we have set “1”==”2″ and this condition is NOT true. Since the if condition is false the JavaScript will skip the firts part and continues with the else code.

<html>
  <body>
    <h2>My JavaScript If Statement</h2>

    <script type="text/javascript">
     if ("1"=="2") 
      {
        document.write("<p>The IF statement is true</p>");
       }
     else
       {
        document.write("<p>The ELSE statement</p>");
        }
     </script>

    </body>
</html>

online tech support online computer help computer technician computer problems computer javascript classes javascript if javascript if else javascript else if javascript if else in javascript if in javascript javascript tutorial design learn web design javascript courses online javascript online course javascript classes javascript tutorial javascript

You can try this JavaScript If Else example on this link.

The Third syntax has additional condition block ELSE IF and this used to declare more conditions to the IF statement. When the entire IF and ELSE IF conditions are not true then JavaScript will end up executing the ELSE statement.

if (<your_condition>)
  {
    <execute this code if the condition is true>;
   }
else if (<your_2nd_condition>)
  {
    <execute this code if your second condition is true>;
   }
else
  {
    <otherwise execute this code>;
   }

Now let’s see how works the ELSE IF statement and to do it we have set the first if condition to false and the else if condition is set to true. This example shows how JavaScript skips the first and the last else conditions. The reason is that JavaScript is looking for the first true condition and skips the next ones even if they are true.

<html>
  <body>
    <h2>My JavaScript If Statement</h2>

    <script type="text/javascript">
     if ("1"=="2") 
      {
        document.write("<p>The IF statement is true</p>");
       }
     else if ("2"=="2")
      {
       document.write("<p>The ELSE IF statement is true</p>");
       }
     else
       {
        document.write("<p>The ELSE statement</p>");
        }
     </script>

    </body>
</html>

online tech support online computer help computer technician computer problems computer javascript classes javascript if javascript if else javascript else if javascript if else in javascript if in javascript javascript tutorial design learn web design javascript courses online javascript online course javascript classes javascript tutorial javascript

You can try this JavaScript Else If example on this link.

We will try again two true else if conditions and as the next example shows only the first true condition will be executed and rest ignored.

<html>
  <body>
    <h2>My JavaScript If Statement</h2>

    <script type="text/javascript">
     if ("1"=="2") 
     {
      document.write("<p>The IF statement is true</p>");
      }
     else if ("2"=="2")
     {
      document.write("<p>The 1st ELSE IF statement is true</p>");
      }
     else if ("3"=="3")
     {
      document.write("<p>The 2nd ELSE IF statement is true</p>");
      }
     else
      {
      document.write("<p>The ELSE statement</p>");
       }
     </script>

    </body>
</html>

online tech support online computer help computer technician computer problems computer javascript classes javascript if javascript if else javascript else if javascript if else in javascript if in javascript javascript tutorial design learn web design javascript courses online javascript online course javascript classes javascript tutorial javascript

You can try this Second JavaScript Else If example on this link.



See Also:
Home

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*