JavaScript Number toString() method

Definition and usage

toString() Return the number as a string.

toString() The method can convert a Number object to a string and return the result.

Note

Each JavaScript object has toString() Method.

When it is necessary to display an object as text (such as in HTML) or to use an object as a string, JavaScript uses it internally toString() Method.

Generally, you will not use it in your own code.

Instance

Example 1

Convert a number to a string:

let num = 15;
let text = num.toString();

Try it yourself

Example 2

Convert a number to a string using base 2 (binary):

let num = 15;
let text = num.toString(2);

Try it yourself

Example 3

Convert a number to a string using base 8 (octal):

let num = 15;
let text = num.toString(8);

Try it yourself

Example 4

Convert a number to a string using base 16 (hexadecimal):

let num = 15;
let text = num.toString(16);

Try it yourself

Syntax

number.toString(radix)

Parameter

Parameter Description
radix

Optional. The base to use.

It must be an integer between 2 and 36:

  • Base 2 is binary
  • Base 8 is octal
  • Base 16 is hexadecimal

Return value

Type Description
String As a string number.

Technical details

Return value

String representation of a number. For example, when radix When 2,number It will be converted to a string representation of the binary value.

Thrown

Exception Description
TypeError An exception is thrown when the object calling this method is not a Number.

Browser support

toString() It is a feature of ECMAScript1 (ES1).

All browsers fully support ES1 (JavaScript 1997):

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support