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 »
Definition and Usage
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
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 string | The 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 | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
The only valid solution for almost all possible existing and future cases [input is number, null, undefined, Symbol, anything else] is String[x]
. Do not use 3 ways for simple operation, basing on value type assumptions, like "here I convert definitely number to string and here definitely boolean to string".
Explanation:
String[x]
handles nulls, undefined, Symbols, [anything] and calls .toString[]
for objects.
'' + x
calls
.valueOf[]
on x [casting to number], throws on Symbols, can provide implementation dependent results.
x.toString[]
throws on nulls and undefined.
Note: String[x]
will still fail on prototype-less objects like Object.create[null]
.
If you don't like strings like 'Hello, undefined' or want to support prototype-less objects, use the following type conversion function:
/**
* Safely casts any value to string. Null and undefined are converted to ''.
* @param {*} value
* @return {string}
*/
function string [str] {
return value == null ? '' : [typeof value === 'object' && !value.toString ? '[object]' : String[value]];
}
The toString[]
method is a built-in method of the JavaScript Number
object that allows you to convert any number
type value into its string
type representation.
How to Use the toString Method in JavaScript
To use the toString[]
method, you simply need to call the method on a number
value. The following example shows how to convert the number value 24
into its string representation. Notice how the value of the
str
variable is enclosed in double quotation marks:
var num = 24;
var str = num.toString[];
console.log[num]; // 24
console.log[str]; // "24"
Convert a number to string with toString[] methodYou can also call the toString[]
method immediately on a number
value, but you need to add parentheses []
to wrap the value or JavaScript will respond with an Invalid or unexpected token
error.
The toString[]
method can also convert floating and negative numbers as shown below:
24.toString[]; // Error: Invalid or unexpected token
[24].toString[]; // "24"
[9.7].toString[]; // "9.7"
[-20].toString[]; // "-20"
Convert any type of numbers with
toString[] methodFinally, the toString[]
method also accepts the radix
or base
parameter. The radix
allows you to convert a number from the decimal number system [base 10] to a string representing the number in other number systems.
Valid number systems for conversion include:
- Binary system [base 2] that has 2 digits, 0 and 1
- Ternary system [base 3] that has 3 digits 0, 1, and 2
- Quaternary system [base 4] that has 4 digits, 0, 1, 2 and 3
- And so on up to the Hexatridecimal system [base 36] that has the combination of Arabic numerals 0 to 9 and Latin letters A to Z
Number.toString[radix];
The syntax for toString[] method, accepting radix parameterThe radix
parameters accept a number
type data with values ranging from 2
to 36
. Here's an example of converting the decimal number 5
to its binary number [base 2] representation:
var str = [5].toString[2];
console.log[str]; // "101"
Converting
decimal number to binary number with toString[] methodThe decimal number 5
from the code above is converted to its binary number equivalent of 101
and then converted to a string.
How to Use Other Data Types with the toString[] Method
Aside from converting the number
type, the toString[]
method is also available for converting other data types into their string representations.
For
example, you can convert an array
type into its string
representation as follows:
var arr = [ "Nathan", "Jack" ];
var str = arr.toString[];
console.log[str]; // "Nathan,Jack"
Convert an array to string with toString[] methodOr a boolean
type to string
as shown below:
var bool = true;
var str = bool.toString[];
console.log[str]; // "true"
But I think you will most often use the toString[]
method to convert a number
to a string
instead of the others. That's what I usually do, too :]
Thanks for reading this tutorial
You may also be interested in other JavaScript tutorials that I've written, including Rounding Numbers with toFixed[]
Method and Calculating Absolute Value with Math.abs[]
. They are two of the most commonly asked JavaScript problems.
I also have a free newsletter about web development tutorials [mostly JavaScript-related].
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started