Web/JavaScript/Reference/Global objects/Number/toString
The toString() method returns a string representing the specified Number object.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
numObj.toString([radix])
Parameters
radixOptional- An integer in the range
2through36specifying the base to use for representing numeric values.
Return value
A string representing the specified Number object.
Exceptions
RangeError- If
toString()is given aradixless than2or greater than36, aRangeErroris thrown.
Description
The Number object overrides the toString() method of the Object object. (It does not inherit Object.prototype.toString()). For Number objects, the toString() method returns a string representation of the object in the specified radix.
The toString() method parses its first argument, and attempts to return a string representation in the specified radix (base). For radices above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), a through f are used.
If the radix is not specified, the preferred radix is assumed to be 10.
If the numObj is negative, the sign is preserved. This is the case even if the radix is 2; the string returned is the positive binary representation of the numObj preceded by a - sign, not the two's complement of the numObj.
If the numObj is not a whole number, the 'dot' sign is used to separate the decimal places.
Examples
Using toString
let count = 10
console.log(count.toString()) // displays '10'
console.log((17).toString()) // displays '17'
console.log((17.2).toString()) // displays '17.2'
let x = 6
console.log(x.toString(2)) // displays '110'
console.log((254).toString(16)) // displays 'fe'
console.log((-10).toString(2)) // displays '-1010'
console.log((-0xff).toString(2)) // displays '-11111111'
Specifications
| Specification |
|---|
| ECMAScript (ECMA-262)The definition of 'Number.prototype.tostring' in that specification. |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toString
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox
Full support 1 |
IE
Full support 3 |
Opera
Full support 4 |
Safari
Full support 1 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
nodejs
Full support 0.1.100 |
Legend
- Full support
- Full support
See also
Number.prototype.toString() by Mozilla Contributors is licensed under CC-BY-SA 2.5.