Number.prototype.toString()

toString(radix)
Returns: string · Added in vES1 · Updated March 13, 2026 · Number
number string conversion

The toString() method returns a string representing the number. Optionally, you can specify a radix (base) to represent the number in different number systems.

Syntax

num.toString(radix)

Parameters

ParameterTypeDescription
radixnumberThe base of the number system (2-36). Defaults to 10.

Return Value

A string representation of the number in the specified radix.

Examples

Decimal (Default)

const num = 255;

num.toString();      // "255"
(42).toString();     // "42"
(0).toString();      // "0"

Binary (Base 2)

const num = 255;

num.toString(2);           // "11111111"
(128).toString(2);         // "10000000"
(15).toString(2);          // "1111"

Octal (Base 8)

const num = 255;

num.toString(8);           // "377"
(64).toString(8);          // "100"
(8).toString(8);           // "10"

Hexadecimal (Base 16)

const num = 255;

num.toString(16);          // "ff"
(255).toString(16);        // "ff"
(16).toString(16);         // "10"
(0).toString(16);          // "0"

Using Letters for Bases Above 16

(35).toString(36);          // "z" (z = 35 in base 36)
(36).toString(36);         // "10" (36 = 1*36 + 0)
(100).toString(36);        // "2s" (2*36 + 28 = 100, s = 28)

Practical Example: Color Conversion

function rgbToHex(r, g, b) {
  const toHex = (n) => {
    const hex = n.toString(16);
    return hex.length === 1 ? 0 + hex : hex;
  };
  return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
}

rgbToHex(255, 128, 0);   // "#ff8000"
rgbToHex(0, 0, 0);       // "#000000"
rgbToHex(255, 255, 255); // "#ffffff"

Practical Example: Binary Flags

const FLAG_READ = 1;    // 0001
const FLAG_WRITE = 2;   // 0010
const FLAG_EXEC = 4;    // 0100

const permissions = FLAG_READ | FLAG_WRITE;  // 3 (0011)

console.log(permissions.toString(2));  // "11"

Edge Cases

(-255).toString(16);    // "-ff"
(0).toString(2);       // "0"
(1.5).toString();      // "1.5"

See Also