3 ms·
Many of those examples also make sense when you consider what you're actually asking for. For example, Arrays have no + operator. So []+[] requires that [] be
by nandhp 13y ago
Many of those examples also make sense when you consider what you're actually asking for.
For example, Arrays have no + operator. So []+[] requires that [] be converted to a string first. The string representation of [] is "" (although some interpreters use "[]" or "[Array]" for clarity), so []+[]==""+""=="".
Similarly, he used the example []+{}, and claimed it was [object Object]. This actually wrong (at least in Chrome); it's actually "[object Object]", a string. The reason for this is the same as the previous example.
However, there is a different reason why {}+[]==0, given that "[object Object]"+[] == "[object Object]". There turns out to be something special about {} at the beginning of a statement (It's a block, or something). Thus, {}+[] is like {};+[], where +[] causes [] to be converted to a number: 0. Compare: {}=={} (syntax error, unexpected token ==). Yes, this one is actually confusing. (However, expressions are not usually evaluated in void context.)
He also observes that ",,,,,,,,,,,,,,," is a bad representation of Array(16). It is, since the undefined in each element is important, but again, that can be solved by using an interpreter with better string generation. (Or a version of IE that doesn't attempt to be clever and just returns "[Object]" all the time).
So perhaps the moral of the story is: Yes, it's an entertaining talk, but a several of the examples break down because his interpreter is vague.