The natural order of unordered object

JavaScript Object is an unordered collection of properties

No, I did not forget promising that the next post will me Map #2, but while mapping the differences, I have seen so many subjects that need deeper diving, that I decided to address them, and only then to return to JavaScript map. And one of them is the claim that you can not trust the order of elements in an object. Since I’m a trusty person, this time I’ll be digging into the natural order of the universe.

Let’s start by diving directly into a messy example:

let o = {
   3:'a',
   a:'b',
   [Symbol('symbol')]:'c',
   '2':'d'
};
//   o = {
//      2: "d",
//      3: "a", 
//      a: "b", 
//      Symbol(symbol): "c"
//   }

1. Numbers:

“In the beginning, God brought the numbers, Thus the numbers were completed in their vast ordered array”. And since objects convert all keys into a string, both numeric key, or string that contains numeric value is the same and comes first. Adding or deleting a number will not break the natural order of numbers. If someone finds an explanation of how the numbers are excluded and sorted numerically, please share it with me.

o[1] = 'e';
o['4'] = 'f';
//   o = {
//      1:"e",
//      2: "d",
//      3: "a",
//      4: "f", 
//      a: "b", 
//      Symbol(symbol): "c"
//   }

2. Strings:

After the magic of the natural order of numbers, JavaScript enumerates on the rest of the keys, and returning all the string’ish keys, one by one, according to the order they had been placed into the object.

o.b='f';
//   o = {
//      1:"e",
//      2: "d",
//      3: "a", 
//      a: "b",
//      b: "f",
//      Symbol(symbol): "c"
//   }

3. Symbols

Last come the symbols, that by definition are for metaprogramming, therefore are arrogant, and therefore needs to stay until the end and think of what they had done and repent  🙂