DEV Community

codeWithNithin
codeWithNithin

Posted on

3 simple ways to convert number to string in JS

Following code is the best way to convert number to string in JS

const method1 = (value) => String(value);
const method2 = (value) => value.toString();
const method3 = (value) => value + '';

console.log(method1(10));
console.log(method2(10));
console.log(method3(10));
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
nombrekeff profile image
Keff

I will add a couple more:

const method4 = (value) => `${value}`;

// For floating values, if we want to get a specific amount of decimals
const method5 = (value, decimals = 2) => value.toFixed(decimals);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
codewithnithin profile image
codeWithNithin

thank you