Skip to content

JavaScript convert number to string | Example code

  • by

Use the toString() function to convert numbers to strings in JavaScript. This 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.

Ways to Convert a number to a string:-

  1. String(n)
  2. n.toString()
  3. ""+n
  4. n+""

Example convert number to string in JavaScript

Simple example code use toString(). Use it when needs to be displayed as text.

<!DOCTYPE html>
<html>

<body>

  <script>

    var num = 24;
    var str = num.toString();

    console.log(num); 
    console.log(typeof(str))

  </script>

</body>
</html>

Output:

JavaScript convert number to string

Another example

This method also converts floating and negative numbers.

20.toString(); // Error: Invalid or unexpected token
(20).toString(); // "20"
(10.7).toString(); // "10.7"
(-20).toString(); // "-20"

Do comment if you have any doubts or suggestions on this JS conversion of number topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *