Convert string to Number
Here are 5 simple ways to convert a string into a number in JavaScript.
1. Using String(): Convert a number to a string using the String() function.
2. Using toString(): Use the number's .toString() method.
3. Using Template Literals: Wrap the number in ${} inside a string.
4. Using Concatenation: Add an empty string + "" to the number.
5. Using JSON.stringify(): Serialize the number using JSON.stringify().
Example Code:
let num = 123;
console.log(String(num)); // "123"
console.log(num.toString()); // "123"
console.log(`${num}`); // "123"
console.log(num + ""); // "123"
console.log(JSON.stringify(num)); // "123"
Convert Number to String
Here are 5 simple ways to convert a number into a string in JavaScript.
1. Using String(): Convert a number to a string using the String() function.
1. Using String(): Convert a number to a string using the String() function.
3. Using Template Literals: Wrap the number in ${} inside a string.
4. Using Concatenation: Add an empty string + "" to the number.
5. Using JSON.stringify(): Serialize the number using JSON.stringify().
Example Code:
let num = 123;
console.log(String(num)); // "123"
console.log(num.toString()); // "123"
console.log(`${num}`); // "123"
console.log(num + ""); // "123"
console.log(JSON.stringify(num)); // "123"
Created By : Muhammad Bakhtawar khan 03200276941