Convert a Number to a String in JavaScript

This example shows you two ways to convert a number to a string in JavaScript.

The first way, we use toString() method. And the second way, we add an empty string to the variable. The code looks like below:

app.js
var age = 18;

var ageString1 = age.toString();
var ageString2 = age + '';

console.log(ageString1, typeof ageString1); // 18 string
console.log(ageString2, typeof ageString2); // 18 string

0 0 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x