I recently came across a bug on a project due to a quirky behavior of Javascript.
Consider the following operations:
let a = {}
a['foo'] = "this"
a['bas'] = "that"
a['bar'] = "meh"
let b = {}
b[1] = "this"
b[3] = "that"
b[2] = "meh"
let c = {}
c[' ' + 1] = "this"
c[' ' + 3] = "that"
c[' ' + 2] = "meh"
let d = {}
d[1 + ''] = "this"
d[3 + ''] = "that"
d[2 + ''] = "meh"
console.log(a, b, c, d)
If you notice, a
and c
had the keys in the exact same fashion that they were assigned to the objects, however, b
and d
appear to have the keys sorted in ascending order.
Any ideas why?