首页 > 解决方案 > Javascript 中的 parseInt() 是如何工作的?

问题描述

    console.log(parseInt('01abbb')) // 1
    console.log(parseInt('31xyz'))  // 31
    console.log(parseInt('zyz31'))  // NaN
    console.log(parseInt('31xyz1')) // 31

是否parseInt()忽略字符恰好不是整数的索引中的后缀?

标签: javascript

解决方案


从字符串转换为数字时,您只会得到数字直到有意义的地方。

console.log(parseInt('01abbb')) // 1 -> it is started by 01 before chars
console.log(parseInt('31xyz'))  // 31 -> it is started by 31 before chars
console.log(parseInt('zyz31'))  // NaN -> it is not started by numbers
console.log(parseInt('31xyz1')) // 31 -> it is started by 31 before chars

推荐阅读