When I input the following code into my Chrome console:
var object = {0:0, 1:1}
I am able to retrieve the values by calling object[0] and object[1]. Surprisingly, even when I use object["0"] and object["1"], I still get the same results. Then, if I redefine the object as:
var object = {"0":0, "1":1}
It turns out that all four calls work without any issues. However, things take an unexpected turn when I redefine the object like this:
var object = {a:0, 1:1}
Now, when I try to call object[a], a ReferenceError is thrown stating "a is not defined". Strangely, using object["a"] returns 0 even though the property name in the declaration is not a string. It seems like JavaScript assumes I am referencing a non-existent variable in the first example. But why do both object[0] and object["0"] yield proper results? Could it be that JavaScript automatically converts numbers since they can't represent variable names? What are the underlying rules governing this behavior, and does it apply universally or only within object bracket notation?