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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

number.toString(radix)

Parameters

ParameterDescription
radix optional Base from 2 to 36. Default: 10

Return value

A string.

Examples

Different basesTry it yourself
const n = 255;
console.log(n.toString(), n.toString(2), n.toString(16), n.toString(36));

Output

255 11111111 ff 73
RGB to a hex colourTry it yourself
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

VersionChange
JavaScript ES1Added

In other languages

Was this page helpful?