简体   繁体   English

parseInt("08")(无基数)在 Chrome 中返回 8,在 Firefox 中返回

[英]parseInt("08") (no radix) returns 8 in Chrome and Firefox

I was showing my colleague why you must always specify a radix with parseInt and I was surprised to see that parseInt("08") returns 8 in both the console of Firefox and Chrome我向我的同事展示了为什么您必须始终使用parseInt指定基数,我很惊讶地看到parseInt("08")在 Firefox 和 Chrome 的控制台中都返回8

shouldn't it be an octal and return 0 ?它不应该是八进制并返回0吗?

parseInt('08') 返回 8

This is a question for old timers这是老前辈的问题

Before 2011, parseInt("08") used to return 0 as parseInt would determine 08 was an octal as it started with an 0 2011 年之前, parseInt("08")用于返回0 ,因为parseInt会确定08是八进制,因为它以0开头

As @Pointy pointed out, the radix being octal for string starting with a 0 has been deprecated for a long time now正如@Pointy 指出的那样,以0开头的字符串的radix是八进制的,现在已经弃用了很长时间

As the radix used in this case depends on the implementation, you should specify a radix ( parseInt("08", 10) because there is no way to be sure whether the octal or decimal algorithm will be used由于在这种情况下使用的基数取决于实现,因此您应该指定一个基数 ( parseInt("08", 10)因为无法确定将使用八进制还是十进制算法

Sources:资料来源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM