JavaScript toString() Method
This tutorial covers the toString() method for JavaScript's Number object. As expected, the toString() method returns a string representation of the number on which it is applied. It is, however, possible to pass numerical arguments to Number.toString() and control the notation of the number.
The toString() method accepts a base as argument: it must be at least 2 and at most 36. In the absence of any argument, toString() will convert the number to base 10 (no visible change when we already work in base 10).
The toString() method is not alone in returning string representations of a number: ironically, all JavaScript Number methods return a string.
For more information, see the tutorials on toFixed(), toPrecision(), or toExponential().
The toString()
method is not exclusive to JavaScript's Number object: it is inherited from the "Object" object, and all data types (and custom objects) inherit the methods and properties of the Object object.
This tutorial specifically examines the toString() method as it pertains to the JavaScript Number object.
Using the toString() Method without Argument
The toString() method accepts an optional numerical argument. When no argument is passed, toString() simply returns the base 10 representation of the number on which it is applied. We work by default in base 10, which is why we may not notice that the toString() method also performs a base conversion.
// Declare a number in base 10
var numex = 3.14;
alert( numex.toString() );
Our script simply returns the number, as it was, in base 10:
The same script now assigns an hexadecimal (base 16) value to our numerical variable. In JavaScript, numbers prefixed with "0x" indicate that the number is expressed in hexadecimal (like anonymous colors in HTML and CSS).
// Declare a number in base 16
var numex = 0xF;
alert( numex.toString() );
This time, the toString() method did not simply returned a string representation of our number. Again, this is because the Number.toString() method returns a string representation of the number in base 10, when no argument is passed (i.e. when no base is specified).
The script below yields the same result: (notice the base 10 argument)
var numex = 0xF;
alert( numex.toString(10) );
Using the toString() Method with Arguments
The Number.toString()
method accepts a numerical argument, whose value is at least 2 (base 2) and at most 36 (base 36). We know from our previous script that "0xF" is the number 15 expressed in base 16.
var numex = 15;
alert( numex.toString(16) );
As expected, JavaScript returned the letter F: (case-insensitive)
You may have noticed the absence of the "0x" prefix. "0x" is only used to instruct JavaScript to use base 16; "0x" is not part of the number.
To close, remember that the JavaScript toString() method applied on a Number object performs two functions: before returning a string representation of the number, it performs a base conversion (conversion to base 10 if no argument is supplied).
No difference is noticeable when you use the default base 10, since converting a base 10 number to base 10 produces a base 10 number.
Test the Number.toString() Method
Interactively test the Number.toString() method by editing the JavaScript code below and clicking the Test Number.toString() method button.
Browser support for JavaScript Number toString() method | |||
---|---|---|---|