Skip to content
RSS feed tkhwang on GitHub tkhwang on Twitter

How to convert string to number in javascript

✨ Basic

Of course, it works.

const num = "123";

Number(num) + // 123
  +num; // 123

✨ Convert by character

Sometimes it's necessary to convert the string number by character.

const strNumber = "123";
let num = 0;
for (const n of strNumber) {
  num = num * 10 + Number(n);
}

πŸ—’οΈ Typical problems