HTML and CSS Reference
In-Depth Information
EXAMPLE 5.3 ( CONTINUED )
1
<script type="text/javascript">
2
var num1 = 5;
var num2 = 7;
3
var result = num1 + num2;
4
document.write("num1 + num2 = "+ result );
5
result = result + (10 / 2 + 5);
6
document.write("<br />12 + (10 / 2 + 5) = " + result );
7
</script>
</h3>
</body>
</html>
EXPLANATION
1
This is the start of a JavaScript program.
2
Variables num1 and num2 are declared and assigned values 5 and 7, respectively.
3
The variable result is assigned the sum of num1 and num2 .
4
The results are displayed by the browser. Note that the + sign in this expression is
used to concatenate two strings. When a string is concatenated to a number, Java-
Script converts the number to a string. The value stored in the variable result is
converted to a string and joined to the string on the left side of the + sign.
5
The expression on the right side of the = sign is evaluated and assigned to the vari-
able, result, on the left side of the = sign. (The parentheses are not needed, but
used for clarity.) The browser output is shown in Figure 5.3.
6
The variable result is concatenated to a string and displayed.
7
Script ends here.
Figure 5.3 Output from Example 5.3.
 
Search WWH ::




Custom Search