I am trying to convert string to number but I am unable to do that , after passing each number from foreach
function sum(num){
num.toString().split('').forEach(add);
}
function add(value , key) {
console.log(value); // 2,3
Number(value);
console.log(typeof(value)) // string string
}
sum(23);
In the code - >
After I did Number(value); in function add(value, key) I am still getting string console.log(typeof(value)) // string
You're not assigning it to a new variable / reassigning the variable:
function sum(num){ num.toString().split('').forEach(add); } function add(value , key) { console.log( { value } ); const newValue = Number(value); // or alternatively value = Number(value); console.log( typeof newValue ); } sum(23);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.