Number.prototype.toString()
Returns a string representing the number in the specified radix (base).
Syntax
num.toString(radix) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
radix | number | No | An integer between 2 and 36 giving the base. Defaults to 10. |
Returns
string — A string representation of the number in the given base.
Throws
RangeError— radix is not between 2 and 36.
Examples
console.log((255).toString(16));
Output
ff
console.log((10).toString(2));
Output
1010
Notes
Commonly used for hex/binary conversion. When calling on a numeric literal use
parentheses or a space (`255 .toString(16)`) so the dot is not parsed as a
decimal point.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.0 |
| safari | 1.0 |
| edge | 12 |
| node | 0.10 |