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.