JavaScript Number toString()

Examples

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 »

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.

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


Syntax

number.toString(radix)

Parameters

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.

Return Value

Type Description
A stringThe number as a string.


More Examples

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 »

Browser Support

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

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.