# Confused about javascript object keys

If I define a new object in javascript, `var myobject = {}`

I will have an empty object as expected. However, I don't understand the logic behind this. Note that everything works, but I don't understand, more teasing than anything else:

``````var myobject = {};                 // Object{}
myobject[001] = "001";         // Object {1: "001"}, So 001 turns to 1
myobject[0099999] = "0099999"; // Object {1: "001", 99999: "0099999"}
//Last line only makes sense after this one (shortened version)βΌ
myobject[0023122] = "0023122"  // Object {... 9810: "0023122" ...}
```

```

I know that I cannot access these properties with `myobject.0023122`

since they start with a number, but I cannot do that `myobject['0023122']`

, so I assume that the number 0023122 is converted to a property with a key `9810`

, as I can do `myobject['9810']`

and get result.

What fun it is that I can do `myobject[99999]`

and `myobject['99999']`

so javascript shouldn't have to reject my key even though I lose my leading zeros. I am not talking about what is wrong and right to do, just what is the reason that a number `0023122`

should be converted to `9810`

, while even `0023123`

gracefully converts to 23123 in the same way it
`0099999`

converts to`99999`

• I assumed it was too big, so I tried with a bigger one.
• If you remove the leading zeros, the key becomes "23123"
+3

source to share

Javascript supports octal numbers:

Positive octal numbers must start with 0 (zero) followed by octal digits.

0023122 (in octal) - 9810 (in decimal)

Thus, any number that has all digits less than 8 but starts at 0 will be converted to octal.

If it has 8 or 9, it will be truncated if it starts at 0.

091 β 91

but

071 β 57

Of course, if you are using string keys, there is nothing to worry about:

`````` myobject["001111"] = "001111"
myobject[001111] = 585
```

```
+5

source

If a number in JS starts with zero, it is considered octal, so the key is converted. `myobject[0099999]`

may throw an error as it is not a valid octal number as it contains 9 (but this may differ depending on the browser / interpreter)

+1

source

In the code, the `myobject[0023122] = "0023122";`

value used as the index for the object is evaluated as an integer primitive.

The 0 prefix causes it to be interpreted as OctalIntegerLiteral, which means 0023122 evaluates to octal. If it were 23122 it would be evaluated as IntegerLiteral. 0x23122 will cause it to evaluate to HexIntegerLiteral.

More details here: http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.3

+1

source

All Articles