Table of contents
1.
Introduction
2.
1. Using toString() Method (Efficient and Simple Method)
2.1.
Syntax
2.2.
Example
3.
2. Using the String() Constructor
3.1.
Syntax
3.2.
Example
4.
3. Concatenating an Empty String (Basic and Simple Way)
4.1.
Syntax
4.2.
Example
5.
4. Using toLocaleString() Method
5.1.
Syntax
5.2.
Example
5.3.
Example
6.
5. Using Lodash _.toString() Method
6.1.
Syntax
6.2.
Example
6.3.
Installation
7.
Syntax  
7.1.
1. `toString()` Method Syntax  
7.2.
2. `String()` Constructor Syntax  
7.3.
3. Concatenation with an Empty String Syntax  
8.
Examples  
8.1.
Example 1: Using `toString()` Method  
8.2.
Example 2: Using String Constructor  
8.3.
Example 3: Concatenating with an Empty String  
9.
Description  
10.
Why Convert Numbers to Strings?  
11.
How JavaScript Handles Conversion  
12.
Key Methods for Conversion  
12.1.
`toString()` Method  
12.2.
`String()` Constructor  
12.3.
Concatenation with an Empty String  
13.
Browser Support  
13.1.
1. `toString()` Method  
13.2.
2. `String()` Constructor  
13.3.
3. Concatenation with an Empty String  
13.4.
Why Browser Support Matters  
14.
Frequently Asked Questions
14.1.
Which is the most efficient way to convert a number to a string in JavaScript?
14.2.
Can toLocaleString() be used for non-numeric values?
14.3.
What is the difference between String() and toString()?
15.
Conclusion
Last Updated: Jan 20, 2025
Medium

Number to String in JavaScript

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Converting numbers to strings in JavaScript is a common task that developers often perform. Whether you are formatting output, storing data, or manipulating numbers as text, understanding the different methods to convert numbers to strings is essential. 

Number to String in JavaScript

In this article, we will cover five simple and efficient ways to perform this conversion: using the toString() method, the String() constructor, concatenating an empty string, the toLocaleString() method, and the Lodash _.toString() method.

1. Using toString() Method (Efficient and Simple Method)

The toString() method is a built-in JavaScript method available for all number objects. It converts a number to its string representation. This is one of the most efficient and straightforward ways to perform the conversion.

Syntax

number.toString();

Example

let number = 123;
let stringNumber = number.toString();
console.log(stringNumber); 
console.log(typeof stringNumber); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

"123"
 "string"


Explanation

In this example, toString() converts the number 123 into a string. The typeof operator confirms that the result is a string.


Note

You can also use toString() to represent numbers in different bases (e.g., binary, octal, hexadecimal):

let binaryString = number.toString(2);
console.log(binaryString);
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"1111011"

2. Using the String() Constructor

The String() constructor is another simple way to convert numbers to strings. It is a global function in JavaScript that works on any data type.

Syntax

String(number);

Example

let number = 456;
let stringNumber = String(number);
console.log(stringNumber);
console.log(typeof stringNumber); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

“456”
"string"


Explanation

Here, the String() constructor converts the number 456 into a string. This method is particularly useful when dealing with non-standard number types like NaN or Infinity:

console.log(String(NaN));
console.log(String(Infinity));
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

“NaN”
"Infinity"

3. Concatenating an Empty String (Basic and Simple Way)

Concatenating a number with an empty string ("") is one of the simplest ways to convert a number to a string. This method is commonly used because of its simplicity.

Syntax

number + "";

Example

let number = 789;
let stringNumber = number + "";
console.log(stringNumber);
console.log(typeof stringNumber); 
You can also try this code with Online Javascript Compiler
Run Code


Output:

“789”
"string"


Explanation

In this approach, the + operator combines the number 789 with an empty string, converting it into a string. This method is straightforward but can be less readable in complex code.

4. Using toLocaleString() Method

The toLocaleString() method converts a number into a string based on the local language settings. It is particularly useful for formatting numbers with commas, decimal points, or other locale-specific styles.

Syntax

number.toLocaleString(locales, options);

 

  • locales: Optional parameter specifying the language (e.g., "en-US").
     
  • options: Optional object to customize the output (e.g., style, currency).

Example

let number = 1234567.89;
let formattedString = number.toLocaleString("en-US");
console.log(formattedString);
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"1,234,567.89"


Explanation

In the example, toLocaleString() formats the number 1234567.89 with commas as per the US English locale.

Example

You can use additional options to format the string further:

let currencyString = number.toLocaleString("en-US", { style: "currency", currency: "USD" });
console.log(currencyString); 
You can also try this code with Online Javascript Compiler
Run Code


Output

"$1,234,567.89"

5. Using Lodash _.toString() Method

The Lodash library provides a utility function, _.toString(), for converting values to strings. It handles edge cases better than native methods.

Syntax

_.toString(value);

Example

let _ = require('lodash');
let number = 123;
let stringNumber = _.toString(number);
console.log(stringNumber); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"123"

 

Explanation

Here, Lodash's _.toString() converts the number 123 into a string. It is particularly helpful when dealing with null, undefined, or complex objects:

console.log(_.toString(null)); 
console.log(_.toString(undefined)); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

""
""

Installation

To use Lodash, you need to install it first:

npm install lodash

Syntax  

Understanding the syntax of each method is crucial for using them effectively in your code. Let’s take a look at the syntax for the three main methods of converting numbers to strings in JavaScript.  

1. `toString()` Method Syntax  

The `toString()` method is called on a number & optionally takes a parameter to specify the base (e.g., binary, hexadecimal).  

number.toString([radix]);  

 

  • - `number`: The number you want to convert to a string.  
     
  • - `radix` (optional): An integer between 2 & 36 that represents the base of the numeral system. If omitted, the default is base 10.  
     

Example:  

let number = 15;  
let binaryString = number.toString(2); // Convert to binary  
let hexString = number.toString(16); // Convert to hexadecimal  
console.log(binaryString);  
console.log(hexString);
You can also try this code with Online Javascript Compiler
Run Code


Output:

"1111" 
 "f"  


 Here’s what’s happening:  

1. We declare a variable `number` & assign it the value `15`.  
 

2. We use `toString(2)` to convert the number to a binary string.  
 

3. We use `toString(16)` to convert the number to a hexadecimal string.  
 

4. The results are stored in `binaryString` & `hexString`.  
 

5. When we log these variables, we get `"1111"` (binary) & `"f"` (hexadecimal).  

2. `String()` Constructor Syntax  

The `String()` constructor takes a value as an argument & converts it to a string.  

String(value);  
  • - `value`: The value you want to convert to a string. This can be a number, boolean, object, or any other data type.  


Example:   

let number = 99;  
let stringNumber = String(number);  
console.log(stringNumber); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"99"  


Let’s see how it works:  

1. We declare a variable `number` & assign it the value `99`.  
 

2. We pass the number to the `String()` constructor.  
 

3. The result is stored in the variable `stringNumber`.  
 

4. When we log `stringNumber`, it shows `"99"` as a string.  

3. Concatenation with an Empty String Syntax  

Concatenating a number with an empty string forces JavaScript to convert the number to a string.   

number + "";   

 

  • `number`: The number you want to convert to a string.  
     
  •  `""`: An empty string that triggers the conversion.  
     

Example:   

let number = 123;  
let stringNumber = number + "";  
console.log(stringNumber); 
You can also try this code with Online Javascript Compiler
Run Code


Output:

"123"  


 Let’s see how it works:  

1. We declare a variable `number` & assign it the value `123`.  
 

2. We concatenate the number with an empty string `""`.  
 

3. The result is stored in the variable `stringNumber`.  
 

4. When we log `stringNumber`, it shows `"123"` as a string.  

Examples  

Example 1: Using `toString()` Method  

The `toString()` method is one of the simplest ways to convert a number to a string. It works on any number & converts it into a string representation.  

let number = 42;  
let stringNumber = number.toString();  
console.log(stringNumber); 
console.log(typeof stringNumber);
You can also try this code with Online Javascript Compiler
Run Code


Output:

“42”
 "string"  


In this code: 

1. We declare a variable `number` & assign it the value `42`.  
 

2. We use the `toString()` method to convert the number to a string.  
 

3. The result is stored in the variable `stringNumber`.  
 

4. When we log `stringNumber`, it shows `"42"` as a string.  
 

5. Using `typeof`, we confirm that the type of `stringNumber` is now `"string"`.  


This method is straightforward & works for all numbers, including integers & decimals.  

Example 2: Using String Constructor  

Another way to convert a number to a string is by using the `String()` constructor. This method explicitly converts the number into a string.  

let number = 3.14;  
let stringNumber = String(number);  
console.log(stringNumber); 
console.log(typeof stringNumber); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"3.14"  
"string"  


In this code: 

1. We declare a variable `number` & assign it the value `3.14`.  
 

2. We pass the number to the `String()` constructor.  
 

3. The result is stored in the variable `stringNumber`.  
 

4. When we log `stringNumber`, it shows `"3.14"` as a string.  
 

5. Using `typeof`, we confirm that the type of `stringNumber` is now `"string"`.  


This method is useful when you want to make the conversion explicit & clear in your code.  

Example 3: Concatenating with an Empty String  

A quick & easy way to convert a number to a string is by concatenating it with an empty string. This forces JavaScript to treat the number as a string.  

let number = 100;  
let stringNumber = number + "";  
console.log(stringNumber); 
console.log(typeof stringNumber); 
You can also try this code with Online Javascript Compiler
Run Code


Output:

"100"  
"string"  


Let’s see how it works:  

1. We declare a variable `number` & assign it the value `100`.  
 

2. We concatenate the number with an empty string `""`.  
 

3. The result is stored in the variable `stringNumber`.  
 

4. When we log `stringNumber`, it shows `"100"` as a string.  
 

5. Using `typeof`, we confirm that the type of `stringNumber` is now `"string"`.  


This method is concise & often used in quick conversions.  

Description  

Now that we’ve seen some examples let’s look into how number-to-string conversion works in JavaScript. Understanding the basic concepts will help you choose the right method for your specific needs.  

Why Convert Numbers to Strings?  

In JavaScript, numbers & strings are different data types. While numbers are used for calculations, strings are used for text manipulation. Sometimes, you need to convert a number to a string to:  

1. Display it in a user interface (e.g., showing a number in a text field).  

2. Concatenate it with other strings (e.g., creating a sentence with dynamic values).  

3. Store it in a format that requires strings (e.g., JSON or APIs).  

How JavaScript Handles Conversion  

JavaScript is a dynamically typed language, which means it can automatically convert data types in certain situations. However, relying on automatic type coercion can sometimes lead to unexpected results. That’s why it’s better to explicitly convert numbers to strings when needed.  

Key Methods for Conversion  

1. `toString()` Method: Converts a number to a string. It can also take an optional parameter to specify the base (e.g., binary, hexadecimal).  
 

2. `String()` Constructor: Explicitly converts a number to a string.  
 

3. Concatenation with an Empty String: A quick way to force conversion by combining a number with an empty string.  


Let’s understand each method in more detail.  

`toString()` Method  

The `toString()` method is available on all number values. It converts the number to a string & optionally allows you to specify a base for the conversion.  

let number = 10;  
let binaryString = number.toString(2); // Convert to binary  
let hexString = number.toString(16); // Convert to hexadecimal  
console.log(binaryString);  
console.log(hexString);
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"1010" 
"a"  


In this code:  

1. We declare a variable `number` & assign it the value `10`.  
 

2. We use `toString(2)` to convert the number to a binary string.  
 

3. We use `toString(16)` to convert the number to a hexadecimal string.  
 

4. The results are stored in `binaryString` & `hexString`.  
 

5. When we log these variables, we get `"1010"` (binary) & `"a"` (hexadecimal).  


This method is powerful when you need to represent numbers in different formats.  

`String()` Constructor  

The `String()` constructor is a straightforward way to convert any value to a string. It’s especially useful when you want to make the conversion explicit in your code.  

let number = 25;  
let stringNumber = String(number);  
console.log(stringNumber); 
You can also try this code with Online Javascript Compiler
Run Code


Output:

 "25"  


In this code:

1. We declare a variable `number` & assign it the value `25`.  
 

2. We pass the number to the `String()` constructor.  
 

3. The result is stored in the variable `stringNumber`.  
 

4. When we log `stringNumber`, it shows `"25"` as a string.  


This method is clear & easy to understand, making it a good choice for readability.  

Concatenation with an Empty String  

Concatenating a number with an empty string is a quick & easy way to convert it to a string. This method leverages JavaScript’s type coercion.  

let number = 7;  
let stringNumber = number + "";  
console.log(stringNumber);
You can also try this code with Online Javascript Compiler
Run Code


Output:

"7"  


This is how it works:  

1. We declare a variable `number` & assign it the value `7`.  
 

2. We concatenate the number with an empty string `""`.  
 

3. The result is stored in the variable `stringNumber`.  
 

4. When we log `stringNumber`, it shows `"7"` as a string.  

Browser Support  

When working with JavaScript, it’s important to ensure that the methods you use are supported across all major browsers. Let’s take a look at the browser support for the three main methods of converting numbers to strings.  

1. `toString()` Method  

The `toString()` method is widely supported across all modern browsers & has been part of JavaScript since the early versions.  

Supported Browsers:  

- Google Chrome (All versions)  
 

- Mozilla Firefox (All versions)  
 

- Safari (All versions)  
 

- Microsoft Edge (All versions)  
 

- Internet Explorer (IE6 & above)  

Example:  

let number = 42;  
let stringNumber = number.toString();  
console.log(stringNumber); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"42"  


This method works consistently across all browsers, making it a reliable choice for number-to-string conversion.  

2. `String()` Constructor  

The `String()` constructor is also supported across all major browsers & has been part of JavaScript for a long time.  

Supported Browsers:  

- Google Chrome (All versions)  
 

- Mozilla Firefox (All versions)  
 

- Safari (All versions)  
 

- Microsoft Edge (All versions)  
 

- Internet Explorer (IE6 & above)  


Example:  

let number = 3.14;  
let stringNumber = String(number);  
console.log(stringNumber); 
You can also try this code with Online Javascript Compiler
Run Code


Output:

 "3.14"  


This method is equally reliable & works seamlessly across all browsers.  

3. Concatenation with an Empty String  

Concatenating a number with an empty string is a basic JavaScript operation & is supported in all browsers.  

Supported Browsers:  

- Google Chrome (All versions)  
 

- Mozilla Firefox (All versions)  
 

- Safari (All versions)  
 

- Microsoft Edge (All versions)  
 

- Internet Explorer (IE6 & above)  
 

Example:  

let number = 100;  
let stringNumber = number + "";  
console.log(stringNumber); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"100"  


This method is simple & works universally, making it a popular choice for quick conversions.  

Why Browser Support Matters  

Ensuring browser compatibility is crucial for web development. If a method isn’t supported in a particular browser, your code might break, leading to a poor user experience. By using the methods discussed above, you can be confident that your code will work across all major browsers.  

Frequently Asked Questions

Which is the most efficient way to convert a number to a string in JavaScript?

Using the toString() method is generally the most efficient and widely used way to convert a number to a string.

Can toLocaleString() be used for non-numeric values?

No, toLocaleString() is specifically designed for numeric values and objects like dates. For non-numeric values, it will throw an error.

What is the difference between String() and toString()?

  • String() works on any data type and can handle null and undefined.
     
  • toString() is a method of numbers and objects and cannot be used on null or undefined directly.

By understanding these methods, you can confidently handle number-to-string conversions in JavaScript for a variety of scenarios.

Conclusion

In this article, we explored five ways to convert numbers into strings in JavaScript. These methods include the toString() method, the String() constructor, concatenating with an empty string, the toLocaleString() method, and the Lodash _.toString() function. Each method has its advantages and is suited to different use cases, from simple conversions to locale-specific formatting.

Live masterclass