javascript - 为什么 parseInt(x,0) 与 parseInt(x, 10) 相同?

标签 javascript

为什么 parseInt('60', 10)parseInt('60', 0) 相同?

JavaScript 将 0 基数转换成什么?

最佳答案

来自 MDN ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt )

If the radix is undefined, 0, or unspecified, JavaScript assumes the following:

If the input string begins with "0x" or "0X" (a zero followed by lowercase or uppercase X), radix is assumed to be 16 and the rest of the string is parsed as a hexidecimal number.

If the input string begins with "0" (a zero), radix is assumed to be 8 (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 clarifies that 10 (decimal) should be used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

If the input string begins with any other value, the radix is 10 (decimal).

因此,如果解析的输入从“0”开始,结果取决于浏览器。

关于javascript - 为什么 parseInt(x,0) 与 parseInt(x, 10) 相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56458990/

相关文章:

javascript - 使用 Javascript (Jquery Mobile) 更新 HTML 按钮的样式

javascript - 如果键是数字,它会自动与从 JSON 创建的表中的左侧对齐

meteor - 如何让 onBeforeAction 调用等到 meteor.js 中的函数调用完成?

javascript - keyup 函数执行异常

javascript - 如何使用javascript从优酷播放视频?

javascript - 预选 UI 上的下拉菜单

javascript - 这里的意外 token (Meteor)在哪里?

javascript - Knockout JS 从数组中调用错误的对象

javascript - return 会停止循环吗?

javascript - 范围错误 : Invalid time value