Arrays are objects with special restrictions.
In general, it does not matter for the object whether you write obj["key"] or obj.key . This is why a["length"] works.
However, in addition to the fact that the number is not a valid identifier, a.0 not equivalent to a[0] . Since the notation in parentheses expects a string, an unquoted literal will be considered as the name of the variable (which itself must contain the string). I will demonstrate with a valid identifier:
obj["foo"] = "bar"; obj.foo; // -> bar var baz = "qwert"; obj[baz] = 5; obj.baz // -> undefined obj.qwert // -> 5 // or obj["qwert"] // -> 5
An Array can also have โObjectโ properties (for example, โlengthโ) that you can set and get. So
trees.foo = "bar" console.log(trees.foo) // -> "bar"
works. But these properties do not take into account the length of the array and also do not take into account the typical methods of arrays, such as push and pop, etc.
source share