HTML and CSS Reference
EXAMPLE 5.3 ( CONTINUED )
var num1 = 5;
var num2 = 7;
var result = num1 + num2;
document.write("num1 + num2 = "+ result );
result = result + (10 / 2 + 5);
document.write("<br />12 + (10 / 2 + 5) = " + result );
Variables num1 and num2 are declared and assigned values 5 and 7, respectively.
The variable result is assigned the sum of num1 and num2 .
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.
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.
The variable result is concatenated to a string and displayed.
Script ends here.
Figure 5.3 Output from Example 5.3.