Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert Number to String in JavaScript?
This tutorial will teach us to convert the number to string in JavaScript. Changing the type of variable is called the type conversion of a variable. While coding, the programmer needs to deal with the different data types and might need to convert the variable's data type.
Every programming language has different methods to convert one variable from one data type to another data type, and JavaScript also has some methods, which are explained below.
Using the toString() Method
Concatenating with an empty String
Using the String() Constructor
Using the toString() Method
In JavaScript, the toString() method is useful for changing the variable type to string. It takes a single parameter called a radix, and the default radix is 10. So, it converts numbers to a decimal string. If we pass radix as 2, it converts a number to a binary string, and the same for radix 16, it converts a number to a hexadecimal string.
Syntax
Users can follow the below syntax to use the toString() method with number values.
let number = 14; let string = number.toString(radix);
Parameters
radix ? It is the base for which users want to convert string from number.
Example
In the below example, we have converted the decimal numbers to decimal strings and binary strings by passing the different radix to the toString() method.
<html>
<head>
</head>
<body>
<h2> Convert the number to string in JavaScript. </h2>
<h4> Converting 302 to string using <i> toString() </i> method. </h4>
<div id="string1"> </div>
<h4> Converting 2342 to binary string using <i> toString(2) </i> method. </h4>
<div id="string2"> </div>
</body>
<script>
var string1 = document.getElementById("string1");
var string2 = document.getElementById("string2");
let number = 302;
let result = number.toString();
string1.innerHTML = result + " <br/> ";
string1.innerHTML += typeof result + " <br/> ";
number = 2342;
string2.innerHTML = number.toString(2) + " <br/> ";
string2.innerHTML += typeof number.toString(2);
</script>
</html>
302 string 100100100110 string
Concatenating with an Empty String
We will simply concatenate the empty string with the number variable in this approach. In JavaScript, when we concatenate the string with any type of variable, the whole variable converts to the string.
Syntax
Here is the syntax to concatenate the number with an empty string to convert it to a string.
let number = 23; let str = "" + number;
Example
In the below example, we have simply declared the number variable and concatenated the empty string with the number to convert the whole number to a string.
<html>
<head>
</head>
<body>
<h2> Convert the number to string in JavaScript. </h2>
<h4> Converting different numbers to string by <i> concatenating empty </i> string. </h4>
<div id="string1"> </div>
</body>
<script>
var string1 = document.getElementById("string1");
let number = 122;
let result = '' + number;
string1.innerHTML = "122 in string is : " + result + " <br/> ";
string1.innerHTML += "typeof 122 is : " + typeof result + " <br/> ";
number = -987 + '';
string1.innerHTML += "-987 in string is : " + number + " <br/> ";
string1.innerHTML += "typeof -987 is : " + typeof number;
</script>
</html>
122 in string is : 122 typeof 122 is : string -987 in string is : -987 typeof -987 is : string
Using the String() Constructor
The String() constructor is the constructor for the class String. When the user passes any variables or values to the String() constructor as a parameter, it converts variables to the string.
Syntax
Users can follow the below syntax to use the String() constructor.
let number = 90; let str = String(number);
Example
The below example demonstrates to use of the String() constructor. We have passed the various number to the String() constructor to make it a string.
<html>
<head>
</head>
<body>
<h2> Convert the number to string in JavaScript. </h2>
<h4> Converting 67 to string by <i> String() constructor</i>. </h4>
<div id="string1"> </div>
</body>
<script>
var string1 = document.getElementById("string1");
let number = 67;
let str = String(number);
string1.innerHTML += str + "<br/>";
string1.innerHTML += "type of 67 is: " + typeof str;
</script>
</html>
67 type of 67 is: string
Comparison of Methods
| Method | Syntax | Supports Radix | Performance |
|---|---|---|---|
| toString() | number.toString(radix) | Yes | Good |
| String concatenation | "" + number | No | Fastest |
| String() constructor | String(number) | No | Good |
Conclusion
All three methods effectively convert numbers to strings. Use toString() when you need different number bases, string concatenation for simple conversions due to its speed, and String() constructor for explicit type conversion. The concatenation method is most commonly used due to its simplicity and performance.
