Spot parseInt Radix Issue
找错误 中等
题目
这个 code gives unexpected results for 一些输入. 什么是 issue?
代码
const result = parseInt("08");
console.log(result);
提示
总是 specify radix (基础) 到 avoid ambiguity.
解释
不使用 radix 参数, parseInt 可能解释执行字符串使用 leading zeros as octal (基础 8) in older 环境. parseInt("08") could be 0 (since 8 is not a valid octal digit) instead of 8. Always pass the radix: parseInt("08", 10) guarantees base-10 parsing and returns 8. Modern JavaScript defaults 到基础 10 for 字符串开始使用 0, 但 specifying radix 是 still 最佳实践.
相关资源
相关课程
- JavaScript 数据类型
学习关于 parseInt.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。