Number.prototype.toString
Converts a number to a string, including in another numeral system.
Available since: ES1
Syntax
number.toString(radix)Parameters
| Parameter | Type | Description |
|---|---|---|
| radix | number | Optional. The base of the numeral system, from 2 to 36. 10 by default. |
Return value
stringThe number as a string.
Examples
A number into a string:
const n = 255;
console.log(n.toString() + '!');Output:
255!Binary and hexadecimal notation:
const n = 255;
console.log(n.toString(2));
console.log(n.toString(16));Output:
11111111
ffNotes
On a numeric literal you need two dots or parentheses: 255..toString(16) or (255).toString(16). The reverse conversion is parseInt('ff', 16).
See also
- parseInt — Converts a string to an integer.
- String() — Converts any value to a string.
- Number.prototype.toFixed — Formats a number with a given number of decimal places.
Related recipes
In the tutorial
- Type conversion — How to turn a string into a number and back in JavaScript — Number, String, Boolean, parseInt and implicit conversion.