Tutorialspoint

JavaScript if statement

<html>
   <body>
      <script>
         var age = 25;
         if( age > 18 ){
            document.write("Eligible to vote!");
         }
      </script>
      <p>Set the variable to different value and then try...</p>
   </body>
</html>

JavaScript if...else if... statement

<html>
   <body>
      <script>
         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>EconomicsBook</b>");
         }
         else{
            document.write("<b>Unknown Book</b>");
         }
      </script>
   </body>
<html>

JavaScript Switch...case statement

<html>
   <body>
      <script>
         var grade = 'A';
         document.write("Entering switch block<br />");
         switch(grade) {
            case'A': document.write("Good job <br />");
            break;

            case'B': document.write("Pretty good <br />");
            break;

            case'C': document.write("Passed <br />");
            break;

            case'D': document.write("Not so good <br />");
            break;

            case'F': document.write("Failed <br />");
            break;

            default:  document.write("Unknown grade<br />")
         }
         document.write("Exiting switch block");
      </script>
   </body>
</html>

JavaScript NaN

<!DOCTYPE html>
<html>
   <body>
      <button onclick="display()">Check</button>
      <p id="test"></p>
      <script>
         function display() {
            var a = "";
            a = a + isNaN(6234) + ": 6234<br>";
            a = a + isNaN(-52.1) + ": -52.1<br>";
            a = a + isNaN('Hello') + ": 'Hello'<br>";
            a = a + isNaN(NaN) + ": NaN<br>";
            a = a + isNaN('') + ": ''<br>";
            a = a + isNaN(0) + ": 0<br>";
            a = a + isNaN(false) + ": false<br>";
            document.getElementById("test").innerHTML = a;
         }
      </script>
   </body>
</html>

JavaScript variable is NaN

<!DOCTYPE html>
<html>
   <body>
      <button onclick="display()">Check</button>
      <p id="test"></p>
      <script>
         function display() {
            var a = "";
            a = a + isNaN(6234) + ": 6234<br>";
            a = a + isNaN(-52.1) + ": -52.1<br>";
            a = a + isNaN('Website') + ": 'Hello'<br>";
            a = a + isNaN(NaN) + ": NaN<br>";
            a = a + isNaN('') + ": ''<br>";
            a = a + isNaN(0) + ": 0<br>";
            a = a + isNaN(false) + ": false<br>";
            document.getElementById("test").innerHTML = a;
         }
      </script>
   </body>
</html>

JavaScript break statement

<html>
   <body>
      <script>
         var x = 1;
         document.write("Entering the loop <br/> ");
         
         while (x < 20) {
            if (x == 5) {
               break; // breaks out of loop completely
            }
            x = x +1;
            document.write( x + "<br/>");
         }
      </script>
   </body>
</html>

JavaScript continue statement

<html>
   <body>
      <script>
         var x = 1;
         document.write("Entering the loop <br /> ");

         while (x < 10) {
            x = x+ 1;
            if (x== 5) {
               continue; // skip rest of the loop body
            }
            document.write( x + "<br />");
         }
         document.write("Exiting the loop!<br /> ");
      </script>
   </body>
</html>

javascript:void(0)” mean

<!DOCTYPE html>
<html>
   <head>
      <title>Understanding JavaScript void(0)</title>
   </head>
   <body>
      <a href="javascript:void(0);" ondblclick="alert('Click it twice!')">Click me not once, but twice.</a>
   </body>
</html>

JavaScript Arithmetic Operators

<html>
   <body>
      <script>
         <!--
            var a = 33;
            var b = 10;
            var c = "Test";
            var linebreak = "<br />";
            document.write("a + b = ");
            result = a + b;
            document.write(result);
            document.write(linebreak);
            document.write("a - b = ");
            result = a - b;
            document.write(result);
            document.write(linebreak);
            document.write("a / b = ");
            result = a / b;
            document.write(result);
            document.write(linebreak);
            document.write("a % b = ");
            result = a % b;
            document.write(result);
            document.write(linebreak);
            document.write("a + b + c = ");
            result = a + b + c;
            document.write(result);
            document.write(linebreak);
            a = ++a;
            document.write("++a = ");
            result = ++a;
            document.write(result);
            document.write(linebreak);
            b = --b;
            document.write("--b = ");
            result = --b;
            document.write(result);
            document.write(linebreak);
         //-->
      </script>
      Set the variables to different values and then try...
   </body>
</html>

JavaScript Comparison Operators

<html>
   <body>
      <script>
         <!--
            var a = 10;
            var b = 20;
            var linebreak = "<br />";
            document.write("(a == b) => ");
            result = (a == b);
            document.write(result);
            document.write(linebreak);
            document.write("(a < b) => ");
            result = (a < b);
            document.write(result);
            document.write(linebreak);
            document.write("(a > b) => ");
            result = (a > b);
            document.write(result);
            document.write(linebreak);
            document.write("(a != b) => ");
            result = (a != b);
            document.write(result);
            document.write(linebreak);
            document.write("(a >= b) => ");
            result = (a >= b);
            document.write(result);
            document.write(linebreak);
            document.write("(a <= b) => ");
            result = (a <= b);
            document.write(result);
            document.write(linebreak);
        //-->
     </script>
     Set the variables to different values and different operators and then try...
   </body>
</html>

1 2 3 4 5 6 7 ... 52 Next
Advertisements
Loading...