Javacript: Integer variables

Now that we know how to manipulate the DOM or change the contents inside HTML elements, let’s use it to illustrate integer variables.

<!DOCTYPE html>
<html>
<head>
 <title>Integer variables</title>
 <script>
   function updateFunction() {
    var x = 10;
    var y = 15
    var z = x + y;
    document.getElementById("int1").innerHTML = x;
    document.getElementById("int2").innerHTML = y;
    document.getElementById("int3").innerHTML = z;
 }
 </script>
</head>
 <body>
   <p id="int1">Integer x</p>
   <p id="int2">Integer y</p>
   <p id="int3">Integer z = x + y</p>
   <br>
  <button onclick="updateFunction()">Click Me</button>
 </body>
</html>

button

When you declare var x = 10, it immediately knows that x is an integer.  In Javascript, we do not have to declare int x = 10.

Notice also how we change the <p> elements using the 3 ids to identify the 3 variables.

JS Fiddle.

Series Navigation<< Javascript: The getElementById() method
Javascript: Functions >>