HTML and CSS Reference
<head><title>The Conversion Functions</title></head>
var num1 = prompt("Enter a number: ","");
var num2 = prompt("Enter another number: ","");
var result = Number(num1) + Number(num2);
// Convert strings to numbers
alert("Result is "+ result );
result=myString + 200;
// String + Number is String
alert("Result is "+ result); // Concatenates 200 to the
// result; displays 20200
alert("Boolean result is "+ Boolean(num2)); // Prints true
The user is prompted to enter a number (see Figure 5.20). Even though the user
variable num1 as a string. On the next line, num2 is assigned another string (see
num1 and num2 have been converted to numbers, the + sign will be used as an ad-
dition operator (rather than a concatenation operator), resulting in the sum of
num1 and num2 . Unless converted to numbers, the string values “30” + “20”
would be concatenated, resulting in 3020 .
The alert box displays the sum of the two numbers entered by the user (see
The variable num1 is converted to a string; its value is assigned to the variable, result .
The value of myString, 20, is concatenated to 200 and assigned to result . The result
is 20200 .
The alert() box displays the result from line 5.
The value of num2 is converted to Boolean, either true or false . Because the value
of num2 is not 0, true is displayed in the alert() dialog box.