2013-11-22 3 views
2

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

,536,913,632 10

편집

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

'console.log (012); // 10' 이런 식으로 이상하게 디버깅 할 때 코드를 줄이면 관련되지 않을 수있는 메서드가 제거됩니다. –

+0

@KevinB - 네 말이 맞아. 해야만 했어. 그 기능이 이상한 행동이라고 생각했습니다. 되지 않도록. – Krishna

답변

3

이 발견이 jQuery를 함께 할 수 없다; 그것은 단지 ECMAScript의 기능/단점입니다 (따라서 JavaScript). 0으로 시작하는 숫자 리터럴은 10 진수가 아닌 8 진수로 해석됩니다 (엄격 모드에서는 예외로 구문 오류가 발생합니다).

012 === 10=== 83을 확인하여 확인할 수 있습니다.

+0

감사합니다. 나는 그것을 계산했다 & 모든 것이 일치하고 있었지만 왜 8 진수인지 확실하지 않았다. – Krishna

+0

'Number ('0123')' –

+0

@NoahHeldman을 사용하여 선행 0을자를 수 있습니다.이 경우에는 _parseInt_ 또는 _parseFloat_를 _Number_보다 사용하는 것이 좋습니다. –

관련 문제