JavaScript (ES2026) reference · Number Methods and Properties
JavaScript number.toString() Method
Convert a number to a string, optionally in another base such as 2 (binary) or 16 (hexadecimal).
JavaScript (ES2026) Since ES1
Syntax
number.toString(radix)
Parameters
| Parameter | Description |
|---|---|
radix optional |
Base from 2 to 36. Default: 10 |
Return value
A string.
Examples
const n = 255;
console.log(n.toString(), n.toString(2), n.toString(16), n.toString(36));
Output
255 11111111 ff 73
const hex = (r, g, b) => "#" + [r, g, b].map((v) => v.toString(16).padStart(2, "0")).join("");
console.log(hex(255, 140, 0));
Output
#ff8c00
Version history
| Version | Change |
|---|---|
| JavaScript ES1 | Added |
In other languages
hex()· Python
Related
Number.parseInt()· Parse an integer from the start of a string in a given base (radix). Always pass the radix, usually 10.string.padStart()· Pad the start of a string until it reaches a target length, for example zero-padding numbers like 007.- JavaScript Numbers · tutorial
- JavaScript Type Conversion · tutorial