JavaScript Number toString()
Examples
Convert a number to a string:
let text = num.toString();
Convert a number to a string, using base 2 (binary):
let text = num.toString(2);
Description
The toString()
returns a number as a string.
Note
Every JavaScript object has a toString()
method.
The toString()
method is used internally by JavaScript
when an object needs to be displayed as a text (like in HTML),
or when an object needs to be used as a string.
Syntax
Parameters
The base to use.
Must be an integer between 2 and 36.
Base 2 is binary
Base 8 is octal
Base 16 is hexadecimal.
Return Value
More Examples
Convert a number to a string, using base 8 (Octal):
let text = num.toString(8);
Convert a number to a string, using base 16 (Hexadecimal):
let text = num.toString(16);
Browser Support
toString()
is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |