Description
- We need a function that can transform a string into a number.
Solution 01
function stringToNumber(str) {
return +str;
}
stringToNumber('1234'); // 1234
console.log(typeof stringToNumber('1234')); // number
Solution 02
function stringToNumber(str) {
return Number(str);
}
stringToNumber('1234'); // 1234
console.log(typeof stringToNumber('1234')); // number
Solution 03
function stringToNumber(str) {
return parseInt(str);
}
stringToNumber('1234'); // 1234
console.log(typeof stringToNumber('1234')); // number
Solution 04
function stringToNumber(str) {
return str | 0;
}
stringToNumber('1234'); // 1234
console.log(typeof stringToNumber('1234')); // number