r/ProgrammerHumor • • Aug 01 '22

>>>print(“Hello, World!”)

Post image
60.8k Upvotes

5.7k comments sorted by

View all comments

Show parent comments

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

2

u/big_bad_brownie Aug 02 '22

/u/danielv123 broke it down further up.

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.

1

u/big_bad_brownie Aug 01 '22

It also only uses the last item in the array if you treat the second square brackets as an array

e.g. [0,1,2,3][0,1,2,n]

returns nth item in the first array