javascript - 为什么 JavaScript 解释器得到 -8 作为语句 parseInt(1/8-9,23) 的结果?

标签 javascript jquery node.js

我不确定为什么会发生这种情况。

parseInt(1/8-9,23);
> -8

任何有关其工作原理的帮助都会很好。

最佳答案

这就是预期的答案。来自 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt :

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values

所以,-8.875解析后,.作为整数无效,因此它被 chop 为 -8 。自 8 < 23 , -8在基地23-8

关于javascript - 为什么 JavaScript 解释器得到 -8 作为语句 parseInt(1/8-9,23) 的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46181348/

相关文章:

javascript - 使 jQuery scrollTop 不留下浏览器历史记录

jquery-ui - jquery嵌套li点击事件多次调用

node.js - 将参数传递给 npm run 脚本

node.js - 如何调试本地运行的 Azure Node.js Web 服务

php - 有没有办法检测用户是否在使用 php 或 javascript 的 wifi 连接上?

javascript - 无法让 RegExp 在 GWT 中匹配

Javascript,如何将标签属性中的所有值放入数组中?

node.js - 指示 Node 库使用包装器进行调用

javascript - 访问循环内 Backbone.js View 上的方法

javascript - 用 <div> 填充 2 点之间的间隙