Take a look at Example 13-4, in which:
n is assigned
the string value
next line prints out its value, and the
typeof operator is used to look up the
n is given the value returned
when the numbers 12345 and 67890 are multiplied together. This value
838102050, but it is a
number, not a string. The type of the variable is then looked up and
Some text is appended to the number
n and the result is displayed.
<script> n = '838102050' // Set 'n' to a string document.write('n = ' + n + ', and is a ' + typeof n + '<br />') n = 12345 * 67890; // Set 'n' to a number document.write('n = ' + n + ', and is a ' + typeof n + '<br />') n += ' plus some text' // Change 'n' from a number to a string document.write('n = ' + n + ', and is a ' + typeof n + '<br />') </script>
The output from this script looks like:
n = 838102050, and is a string n = 838102050, and is a number n = 838102050 plus some text, and is a string
If there is ever any doubt about the type of a variable, or you need to ensure a variable has a particular type, you can force it to that type using statements such as the following (which ...