The empty array inside the array is casted to an empty sting because it calls toString on the array which returns a comma seperated list of all entries. Because its empty it just returns an empty string. That means that its not accessing index 0 its accessing Index empty string. It works if you test it with an object
It’s not trying to access index “” It’s trying to access property “”
If the string !isNaN, it gives you that index. Otherwise, it works as expected when you’re using square brackets to call or assign a property.
Honestly, I think it’s silly that people complain about this kind of thing when you’re clearly abusing the syntax. But also, it is weird that JavaScript doesn’t just tell you “unexpected type” when you give it anything other than a number.
3
u/XNocken Aug 01 '22
The empty array inside the array is casted to an empty sting because it calls toString on the array which returns a comma seperated list of all entries. Because its empty it just returns an empty string. That means that its not accessing index 0 its accessing Index empty string. It works if you test it with an object