Ordered keys in JS objects

Ordered keys?

It seems like JavaScript has ordered keys. Notice that the order of keys are based on insertion order.

> obj = {};
> obj.zulu = 1;
> obj.yankee = 1;
> obj.xray = 1;
> obj
{ zulu: 1, yankee: 1, xray: 1 }

Adding keys

You can add more keys to an object, and it'll always end up in the end.

> obj.bravo = 1;
> obj.alpha = 1;
> obj
{ zulu: 1, yankee: 1, xray: 1, bravo: 1, alpha: 1 }
Next: Is that really what always happens?

Not really

Numeric keys

But there are special cases when the key is a string that is a number. Notice this puts the new key at the beginning:

> obj['5'] = 1;
> obj
{ '5': 1, zulu: 1, yankee: 1, xray: 1, bravo: 1, alpha: 1 }

This is only true for numbers-as-strings. Strings that begin with numbers aren't affected.

> obj['5X'] = 1;
> obj
{ '5': 1, zulu: 1, yankee: 1, xray: 1, bravo: 1, alpha: 1, '5X': 1 }
Next: Let's take a look at how it's sorted.

Sorting behavior

Numeric keys first

The number-like keys are going to be sorted numerically (not lexically) before the other keys. (Hat tip to @cheeaun for the observation on the behavior of numeric sorting!)

> obj = {};
> obj.y = 1;
> obj.x = 1;
> obj['22'] = 1;
> obj['5'] = 1;
> obj['2'] = 1;
> obj
{ '2': 1, '5': 1, '22': 1, y: 1, x: 1 }