JavaScript Number.prototype.toString() Method
The toString() method converts a number to a string, optionally in another base such as binary (2) or hexadecimal (16).
Description
The toString() method returns the number as a string. With a radix, it converts to another base: 2 for binary, 8 for octal, 16 for hexadecimal and up to 36. To call it on a number literal, use two dots or parentheses: 5..toString() or (5).toString().
Without a radix, the result is the shortest string that uniquely identifies the number, so (0.1 + 0.2).toString() shows all the digits needed, "0.30000000000000004". Very large and very small numbers use exponential notation.
Converting to binary or hexadecimal is common when working with colors, bit flags and character codes, and parseInt(str, radix) converts back. For human-readable formatting with separators and fixed decimals, use toLocaleString() or toFixed() instead.
Syntax
number.toString()
number.toString(radix)Parameters
- radixOptional. The base to use, from 2 to 36. Defaults to 10.
Return Value
A string representing the number in the given base.
Exceptions
- RangeErrorThrown if the radix is outside 2 to 36.
Example
Browser Support
The following information will show you the current browser support for the Number.prototype.toString() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





