在控制台中编写数字时的奇怪行为

时间:2013-11-22 20:41:23

标签: javascript jquery

数字的{p> console.log()表现不同。

以下调查结果很有意思。有什么解释吗?

console.log(1) - 1

console.log(01) - 1

console.log(12) - 12

console.log(012) - 10

console.log(0123) - 83

console.log(123) - 123

修改

MDN documentation:

中找到了这个
Integers can be expressed in decimal (base 10), hexadecimal (base 16), and octal (base 8).

Decimal integer literal consists of a sequence of digits without a leading 0 (zero).
Leading 0 (zero) on an integer literal indicates it is in octal. Octal integers can include only the digits 0-7.
Leading 0x (or 0X) indicates hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F.
Octal integer literals are deprecated and have been removed from the ECMA-262, Edition 3 standard (in strict mode). JavaScript 1.5 still supports them for backward compatibility.

1 个答案:

答案 0 :(得分:3)

这与jQuery无关;它只是ECMAScript(以及JavaScript)的特征/怪癖。以0开头的数字文字以八进制解释,而不是十进制(严格模式除外,在这种情况下会产生语法错误)。

您可以通过观察012 === 100123 === 83来验证这一点。