Interesting JavaScript Quirk When Accessing Object With Index
I’ve just discovered an interesting JavaScript quirk which I haven’t seen before. It turns out that indexing an object with a single element array has the exact same effect as indexing with that array’s element.
JavaScript just doesn’t stop amazing developers. There are many quirks I’ve seen, known and exploited. I was so surprised to hav found one that I’ve never seen before. It turns out that accessing object as an associative array works not only with the actual index but also when a single element array is used for index.
See for yourself below:
Of course, the array with two elements returns undefined
as expected.
What is even more insane, is that the array can be of any depth as long as it is a single element array within a single element array within a single element array etc. How weird is that?
Digging deeper, I discovered that it is possible to index an object with arrays on any dimension and that would be equivalent as indexing with the elements joined with a comma. Here’s a demonstration
Again, it does not matter whether the array is deeply nested inside other arrays
I imaging this can actually be quite useful though I’d be afraid that programmers not familiar with this trick would not understand what is happening.
A superfluous google search didn’t answer why this works like that. A comment with an explanation will be greatly appreciated .