How to convert Number to String in JavaScript?

Front End TechnologyJavascriptWeb Development

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 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>

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 number 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>

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>

Users have seen three different approaches to converting the numbers to strings. It is the best approach to concatenate the number with an empty string, and you will find that number is converted to the string without much effort. The toString() method converts the number to a decimal string by default, removing the previous zeros from the number, but the second and third approaches will not do that.

raja
Updated on 10-Aug-2022 12:29:28

Advertisements