gpt4 book ai didi

javascript - 将 'string number' 转换为数字

转载 作者:行者123 更新时间:2023-11-28 12:37:28 24 4
gpt4 key购买 nike

我正在创建一个库,并尝试将字符串发送到函数中并将其作为数字返回,同时创建一个条件以确定发送到函数中的字符串是否是数字,然后再进行转换。

var strNum = function(val){
if (!isNan(val)){
console.log('This is a string that can be converted')
parseInt(val)
return val
}else{
console.log ('This sting is not a "number"');
}
};

这是我所拥有的,但在调试它时出现错误“ReferenceError:isNan 未定义 if (val = !isNan(val)){”我不知道为什么它不起作用!

有什么想法吗?

最佳答案

它是isNaN而不是isNan

JavaScript 中没有名为 isNan 的方法,这就是它抛出引用错误的原因。

关于javascript - 将 'string number' 转换为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15677857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com