How can {} + [] and [] + {} results be different

Possible duplicate:
What is the explanation for these fancy JavaScript actions mentioned in the Wat talk for CodeMash 2012?

Can someone explain to me how those results returning results can be different in javascript:

โ€บ {} + [] ยป 0 โ€บ [] + {} ยป "[object Object]" 
+6
source share
1 answer

This is because {} in the code is not an object literal, but an empty block.

It is analyzed as:

 {}; // empty block + []; // this result is shown in the console 

Compare with ({}) + [] , which gives the same results as [] + {} ; in this case, the bracket {} processed / parsed "in the context of the expression".

There is a bunch of duplicates on SO about this particular dual nature {} (like an expression or a block?), But finding them can be a little tricky.


I found https://meta.stackexchange.com/questions/83911/how-do-i-search-stackoverflow-for-at-keywords-like-private-or-synthesize on Meta, and using Symbolhound, "closest" duplicates that I could find resolved around questions like this (which are related to the need to add brackets when "eval'ing JSON") or this (where using constructs like "{} == false" is a syntax error).

If someone knows the best way to search for SO for this sort question or has a link to such a duplicate, convenient ..

+7
source

Source: https://habr.com/ru/post/923094/


All Articles