w3docs logo
  1. Snippets
  2. JavaScript
  3. How to Convert a Number to a String in JavaScript

How to Convert a Number to a String in JavaScript

There are several built-in methods in JavaScript that convert from an number data type to a String. Let’s discuss each of them.

toString()

The toString() method takes an integer or floating point number and converts it into a String type. There are two ways of invoking this method. If the base number is passed as a parameter to toString(), the number will be parsed and converted to it:

w3docs logo Javascript toString method
let a = 10; console.log(a.toString()); // '10' console.log((60).toString()); // '60' console.log((9).toString(2)); // '1001' (9 in base 2, or binary)

String()

The String() method creates a primitive String type for the number passed to it:

w3docs logo Javascript String method
let a = 10; console.log(String(a)); // '10' console.log(String(52)); // '52' console.log(String(47.62)); // '47.62'

The difference between the String() method and the String method is that the latter does not do any base conversions.

How to Convert a Number to a String in JavaScript

The template strings can put a number inside a String which is a valid way of parsing an Integer or Float data type:

w3docs logo Javascript template strings
let num = 10; let flt = 10.502; let string = `${num}`; // '10' console.log(string); let floatString = `${flt}`; // '10.502' console.log(floatString);

Template literals are string literals that allow embedded expressions. Multi-line strings and string interpolation features can be used with template literals.

Concatenating an Empty String

This method is considered one of the fastest way of converting a number into string.

w3docs logo Javascript converting a number into string
let a = '' + 40 // '40'; console.log(a);

However, be careful as this method sometimes may not return the preferable string:

w3docs logo Javascript concatenating an empty string
let a = '' + 123e-60 // ‘1.23e-58’ console.log(a);

The speed of these above methods differs by browsers.

Do you find this helpful?