Javascript - If...Else


if (expression) {
   Statement(s) to be executed if expression is true
}

<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 (expression) {
   Statement(s) to be executed if expression is true
} else {
   Statement(s) to be executed if expression is false
}

<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 (expression 1) {
   Statement(s) to be executed if expression 1 is true
} else if (expression 2) {
   Statement(s) to be executed if expression 2 is true
} else if (expression 3) {
   Statement(s) to be executed if expression 3 is true
} else {
   Statement(s) to be executed if no expression is true
}

<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:

Post a Comment

Welcome

Lets learn java script