Convert a Number to a String in JavaScript
Last Updated :
15 Nov, 2024
Improve
These are the following ways to Convert a number to a string in JavaScript:
1. Using toString() Method (Efficient and Simple Method)
This method belongs to the Number.Prototype object. It takes an integer or a floating-point number and converts it into a string type.
let a = 20;
console.log(a.toString());
console.log((50).toString());
console.log((7).toString(2));
// (7 in base 2, or binary)
Output
20 50 111
2. Using the String() constructor
The String() constructor accepts an integer or floating-point number as a parameter and converts it into string type.
console.log(String(52));
console.log(String(35.64));
Output
52 35.64
Note: It does not do any base conversations as .toString() does.
3. Concatenating an Empty String(Basic and Simple way)
This is arguably one of the easiest ways to convert any integer or floating-point number into a string.
let a = '' + 50;
console.log(a);
Output
50
4. Using toLocaleString() Method
The toLocaleString() method converts a number into a string, using a local language format.
let n = 92;
let s = n.toLocaleString();
console.log(s);
Output
92
5. Using Lodash _.toString() Method
We are using Lodash _.toString() method that convert the given value into the string only.
const _ = require("lodash");
console.log(_.toString(-0));
Output:
'-0'