JS object will automatically sort numeric keys!

ยท

1 min read

Warning: Objects will not preserve the insertion order. Use Map instead

const obj = {}
obj[2] = 'b'
obj[4] = 'd'
obj[1] = 'a'
console.log(obj) -> // {1: 'a', 2: 'b', 4: 'd'}

But Objects behave differently when the key is not numeric ๐Ÿ™„

const obj = {}
obj['b'] = '2'
obj['d'] = '4'
obj['a'] = '1'
console.log(obj) -> // {b: '2', d: '4', a: '1'}