Convert a number to a string:
let num = 15;
let text = num.toString();
Try it Yourself »
Convert a number to a string, using base 2 (binary):
let num = 15;
let text = num.toString(2);
Try it Yourself »
The toString()
returns a number as a string.
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.
Normally, you will not use it in your own code.
number.toString(
radix)
Parameter | Description |
radix | Optional. The base to use. Must be an integer between 2 and 36. Base 2 is binary Base 8 is octal Base 16 is hexadecimal. |
Type | Description |
A string | The number as a string. |
Convert a number to a string, using base 8 (Octal):
let num = 15;
let text = num.toString(8);
Try it Yourself »
Convert a number to a string, using base 16 (Hexadecimal):
let num = 15;
let text = num.toString(16);
Try it Yourself »
toString()
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!