While reading I found this expression:
_=$=+[],++_+''+$
Curiously, it is a valid expression in javascript
but the most curious thing is that when it is executed, the result is:
10
I tried to make sense of it by breaking the expression down but I didn't get much done.
Why is the result of that expression equal to 10
?
Easy, this expression
It can be written more readably like this
Which can be understood as:
Which is the same as:
Which is the same as:
That to read it easier we can rename variables like:
Than executing it step by step:
Let's do some transformations. I
_
'm going to call what he calls and I'm going to call whata
he calls . I simply renamed these two variables so their original expression is now:$
b
Under this equivalent expression:
,
(comma) operator. This operator evaluates each of its operands from left to right and returns the value of the last expression it evaluated.a = b = +[]
. This expression is a compound assignment expression. The assignment operator=
associates from right to left, so this expression should evaluate toa = (b = +[])
b = +[]
assigns it to a variableb
(which must resolve first within the scope chain: if it exists, it assigns the appropriate value, if it doesn't exist -as in this example- it creates a property calledb
on the global object (in the browser it would be the objectwindow
). That variable is assigned the value+[]
. The unary operator+
is responsible for converting a data type to an integer value. In this case, we try to convert the empty array[]
. When the operator is used in an array+
, Javascript calls to the array methodtoString()
, returning a string representation of the array content. Since the array is empty, it returns the string''
. Now, the ECMA standardmentions that the representation of an empty text string when converted to a number is the integer value 0, therefore, the expressionb = +[]
assignsb
the value 0.a
, which, like the variableb
, if it exists, it is assigned the value, if not, it is created as a property in the global object. So far, what has been done is to create two variables called a and b that are assigned the value 0 and that exist in the global object.,
:++a+''+b
. Due to the appearance of the empty character string''
. the operators+
that appear will act by concatenating strings of characters. This operator evaluates from left to right.++a
. This expression contains the prefix unit increment operator++
: its job is to increment the value of the variable by 1 and return that result. Therefore,a
, which is 0, is now 1 and returns this value.+
concatenates the value obtained by the left operand (which returned the integer value 1) with the empty string''
. This work promotes the integer value 0 to the character string '1'. Finally returns this result.+
concatenates the value obtained by the left operand (which returned the character string '1') with the right operand. The right operand is simply the value of the variableb
, which is 0. Again, the operator+
promotes this integer value to the character string '0', due to concatenation The concatenation of the left operand with the right is '1'+ '0' = '10' and returns this result.,
returns the result that the last expression returned, in this case, it will return the character string '10', which is the result you get.