DEV Community

Discussion on: Become a Javascript Magician with these tricks!

Collapse
 
dwd profile image
Dave Cridland

Converting to a number and string that way is risky, because you're using implicit conversions.

Instead, you can convert a number to a string by using string interpolation:

const stringNumber = `${number}`
Enter fullscreen mode Exit fullscreen mode

Converting to a number can be done two ways - you can use Number or parseInt, depending on the behaviour you need when it's not quite a number:

const numberString = Number('20') // 20
const numberWang = parseInt('20 cows') // 20
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sobhandash profile image
Sobhan Dash

True! I was trying to find other ways to do the conversations. These are the better options clearly.