How do you convert numbers to strings in javascript?

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));
}

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Below is the example of the Number toString() Method.

    • Example: 

    html

    <script type="text/javascript">

        var num=12;

        document.write("Output : " + num.toString(2));         

    script>

    • Output:
    Output:1100

    The toString() method in Javascript is used with a number and converts the number to a string. It is used to return a string representing the specified Number object. Syntax:

    num.toString(base)

    The toString() method is used with a number num as shown in above syntax using the ‘.’ operator. This method will convert num to a string. Parameters Used: This method accepts a single optional parameter base. This parameter specifies the base in which the integer is represented in the string. It is an integer between 2 and 36 which is used to specify the base for representing numeric values. Return Value: The num.toString() method returns a string representing the specified number object. Below are some examples to illustrate the working of toString() method in JavaScript:

    1. Converting a number to a string with base 2: To convert a number to a string with base 2, we will have to call the toString() method by passing 2 as a parameter. 

    html

    <script type="text/javascript">

        var num=213;

        document.write("Output : " + num.toString(2));         

    script>

    1. Output:
    Output:11010101
    1. Converting a number to a string with base 8: To convert a number to a string with base 8, we will have to call the toString() method by passing 8 as a parameter. 

    html

    <script type="text/javascript">

        var num=213;

        document.write("Output : " + num.toString(8));         

    script>

    1. Output:
    Output : 325
    1. Converting a number to a string with base 16: To convert a number to a string with base 16, we will have to call the toString() method by passing 16 as a parameter. 

    html

    <script type="text/javascript">

        var num=213;

        document.write("Output : " + num.toString(16));         

    script>

    1. Output:
    Output : d5
    1.  
    2. When no parameter is passed: If the toString() method is called without passing any parameter then the number will be converted to string without change in BASE. Below is the program to illustrate this: 

    html

    <script type="text/javascript">

        var num=213;

        document.write("Output : " + num.toString());         

    script>

    1. Output:
    Output :213

    Supported Browsers:

    • Google Chrome 1 and above
    • Internet Explorer 3 and above
    • Firefox 1 and above
    • Apple Safari 1 and above
    • Opera 4 and above
    • Edge 12 and above

    JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


    How do you turn a number into a string?

    There are several easy ways to convert a number to a string:.
    int i; // Concatenate "i" with an empty string; conversion is handled for you. ... .
    // The valueOf class method. ... .
    int i; double d; String s3 = Integer.toString(i); String s4 = Double.toString(d);.

    Can a number be a string in JavaScript?

    JavaScript Number toString() The toString() returns a number as a string.

    How do I convert a number to a string in TypeScript?

    Use the String() object to convert a number to a string in TypeScript, e.g. const str = String(num) . When used as a function, the String object converts the passed in value to a primitive string and returns the result. Copied!

    How do you turn an object into a string in JavaScript?

    Stringify a JavaScript Object Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);