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
Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147

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
  • 405,095
  • 59
  • 585
  • 614
3

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

spicavigo
  • 4,116
  • 22
  • 28
2

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

user113716
  • 318,772
  • 63
  • 451
  • 440
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,531
  • 2
  • 36
  • 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
  • 683,504
  • 96
  • 985
  • 979
1

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

millimoose
  • 39,073
  • 9
  • 82
  • 134