How to Convert Integer to String in JavaScript
-
Use
''
to Convert an Integer to a String in JavaScript - Use the Template Literals to Convert an Integer to a String in JavaScript
-
Use the
String()
Function to Convert an Integer to a String in JavaScript -
Use the
toString()
Function to Convert an Integer to a String in JavaScript
Numbers and strings are primitive datatypes in Python. Integers represent numbers, and the string data type defines the textual data. Strings can represent numbers also.
In this tutorial, different methods are discussed on how to a number to a string in JavaScript.
Use ''
to Convert an Integer to a String in JavaScript
Single quotes and double quotes are used to represent a string in programming. In this method, we add ''
before or after the number to convert it to a string. This is one of the fastest methods to convert a number to a string.
Check the code below.
var a = 45;
var b = a + '';
var c = '' + a;
console.log(typeof (b));
console.log(typeof (c));
Output:
string
string
On adding a string, the number gets automatically converted to a string. The typeof()
function here tells the datatype of the variable in JavaScript.
Use the Template Literals to Convert an Integer to a String in JavaScript
Template literals (Template Strings) are string literals that allow us to embed expressions. We can also use this to convert an integer to a string.
Check the code below.
var a = 45;
var b = `${a}`;
console.log(typeof (b));
Output:
string
Use the String()
Function to Convert an Integer to a String in JavaScript
We have used String()
to convert the value of a variable to a string. This method is also called typecasting. Typecasting means changing the data type of one value to another data type.
Check the code below.
var a = 45;
var b = String(a);
console.log(typeof (b));
Output:
string
Use the toString()
Function to Convert an Integer to a String in JavaScript
The method object.toString()
returns the string. We can take the required number object and use it with this function to convert it into a string.
Check the code below.
var a = 45;
var b = a.toString();
console.log(typeof (b))
Output:
string