How do you convert a number to a string in JavaScript?

In JavaScript, you can convert a number to a string using the toString() method or by using string interpolation.

Method 1: Using the toString() method

let num = 42;
let str = num.toString(); // converts number to string
console.log(typeof str); // Output: "string"
console.log(str); // Output: "42"

As you can see in the example above, toString() method is converting a number to a string.

You can also specify the base for the string representation of the number by passing an argument to the toString() method.

For example:

To convert a number to a binary string, you can use num.toString(2).

let num = 42;
let binaryStr = num.toString(2); // converts number to binary string
console.log(binaryStr); // Output: "101010"

Method 2: Using string interpolation:

let num = 42;
let str = `${num}`; // converts number to string
console.log(typeof str); // Output: "string"
console.log(str); // Output: "42"

As you can see in the above example, we can use string interpolation to convert number to string.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: