Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Number.prototype.toString

Converts a number to a string, including in another numeral system.

Available since: ES1

Syntax

number.toString(radix)

Parameters

ParameterTypeDescription
radixnumberOptional. 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
ff

Notes

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

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.

Thanks for reading!

Donate to the site — we work hard for you.