JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Number.toString() Method

Name

Number.toString() Method---convert a number to a string

Availability

Navigator 3.0

Synopsis

number.toString([radix])

Arguments

radix

This optional argument specifies the base that should be used to convert the number. It should be an integer between 2 and 16. If no value is specified, then base 10 is used.

Returns

The string representation of the number in the specified radix.

Description

The toString() method of the Number object converts a Number to a string, using the specified radix or base. If no radix is specified, base 10 is used.

Usage

Because JavaScript automatically converts numeric values to temporary Number objects when needed, you can use the toString() method on numbers, even though they are primitive types rather than true JavaScript objects:

n = 123;
s = n.toString(16);

Note, however, that because of syntactic restrictions in the language, you cannot use the toString() method on numeric literals. You must assign them to variables first; i.e., you cannot rewrite the two lines of code above as:

s = 123.toString(16);

See Also

"Number", "Object.toString()"


Previous Home Next
Number.prototype Book Index Number.valueOf()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
This HTML Help has been published using the chm2web software.