javascript - 为什么我在 node.js 中使用 parseInt 会得到奇怪的结果? (与 chrome js 控制台的结果不同)

标签 javascript node.js google-chrome parseint

我刚刚注意到:

//IN CHROME JS CONSOLE
parseInt("03010123"); //prints: 3010123

//IN NODE.JS
parseInt("03010123"); //prints: 790611

既然都是基于V8,为什么同样的操作会产生不同的结果???

最佳答案

当字符串被传递给 parseInt 时会出现未定义的行为。有一个前导 0,并且您省略了 radix 参数。

An integer that represents the radix of the above mentioned string. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified.

有些浏览器默认以 8 为基数,有些以 10 为基数。我不确定文档对 Node 的看法,但显然它假设以 8 为基数,因为以 8 为基数的 3010123790611 以 10 为底。

你会想要使用:

parseInt("03010123", 10);

关于javascript - 为什么我在 node.js 中使用 parseInt 会得到奇怪的结果? (与 chrome js 控制台的结果不同),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16880327/

相关文章:

javascript - Span 元素不运行来自 jQuery 的动画

javascript - 如何在javascript中转换日期?

javascript - stdio 流 : WriteStream 的值不正确

javascript - 将 vanilla/jquery 元素添加到 React 组件

javascript - Backbone中未知数量和参数属性名称的动态实例化

node.js - 获取远程 IP 地址在 Node 上不起作用

node.js - ReactJS,内部专用 IP 服务器上的 Express 应用程序自托管

google-chrome - 在 Selenium ChromeDriver 中使用 native 消息传递?

google-chrome - SSL Localhost 隐私错误

google-chrome - 在 Google Dart 中使用 handle