0

Possible Duplicate:
Leading zero in javascript

Given you have the following JavaScript snippet:

<script>
   var x = 013;
   console.log(x);
</script>

Why is it that Firebug prints 11?

Community
  • 1
  • 1
stevek-pro
  • 13,672
  • 14
  • 81
  • 134

6 Answers6

4

JavaScript supports the same convention for numeric constants as C and Java (et al), so the leading zero makes that an octal constant. ("13" in base 8 is 8 + 3, or 11.)

Pointy
  • 389,373
  • 58
  • 564
  • 602
3

Its octal value. So it 1*8^1 + 3*8^0=11

spicavigo
  • 3,980
  • 20
  • 28
2

Because you've specified an octal number, but it displays its decimal representation.

user113716
  • 310,407
  • 61
  • 442
  • 435
1

It's interpreting it as octal. Any number that begins in zero is interpreted as an octal (base 8) literal, and octal 13 = 8*1+3 = 11 decimal.

Also, good title.

Dan
  • 10,381
  • 2
  • 34
  • 55
1

It is being interpreted as base 8. And 013 in base 8 is 11 decimal.

In javascript, constant numbers that begin with 0dd or -0dd and are not 0xdd or -0xdd are interpreted as octal (base 8).

You can see it described in the ECMAScript specification on page 231.

jfriend00
  • 637,040
  • 88
  • 896
  • 906
1

013 is an octal constant - it's interpreted in base 8. 1·8 + 3·1 = 8 + 3 = 11

millimoose
  • 37,888
  • 9
  • 76
  • 132