This is valid and returns the string "10"
in JavaScript (more examples here):
console.log(++[[]][+[]]+[+[]])
Why? What is happening here?
This is valid and returns the string "10"
in JavaScript (more examples here):
console.log(++[[]][+[]]+[+[]])
Why? What is happening here?
This one evaluates to the same but a bit smaller
so is evaluates to
So now you got that, try this one:
Then we have a string concatenation
Perhaps the shortest possible ways to evaluate an expression into "10" without digits are:
+!+[] + [+[]]
// "10"-~[] + [+[]]
// "10"//========== Explanation ==========\\
+!+[]
:+[]
Converts to 0.!0
converts totrue
.+true
converts to 1.-~[]
=-(-1)
which is 1[+[]]
:+[]
Converts to 0.[0]
is an array with a single element 0.Then JS evaluates the
1 + [0]
, thusNumber + Array
expression. Then the ECMA specification works:+
operator converts both operands to a string by calling thetoString()/valueOf()
functions from the baseObject
prototype. It operates as an additive function if both operands of an expression are numbers only. The trick is that arrays easily convert their elements into a concatenated string representation.Some examples:
There's a nice exception that two
Objects
addition results inNaN
:+[] evaluates to 0 [...] then summing (+ operation) it with anything converts array content to its string representation consisting of elements joined with comma.
Anything other like taking index of array (have grater priority than + operation) is ordinal and is nothing interesting.
The following is adapted from a blog post answering this question that I posted while this question was still closed. Links are to (an HTML copy of) the ECMAScript 3 spec, still the baseline for JavaScript in today's commonly used web browsers.
First, a comment: this kind of expression is never going to show up in any (sane) production environment and is only of any use as an exercise in just how well the reader knows the dirty edges of JavaScript. The general principle that JavaScript operators implicitly convert between types is useful, as are some of the common conversions, but much of the detail in this case is not.
The expression
++[[]][+[]]+[+[]]
may initially look rather imposing and obscure, but is actually relatively easy break down into separate expressions. Below I’ve simply added parentheses for clarity; I can assure you they change nothing, but if you want to verify that then feel free to read up about the grouping operator. So, the expression can be more clearly written asBreaking this down, we can simplify by observing that
+[]
evaluates to0
. To satisfy yourself why this is true, check out the unary + operator and follow the slightly tortuous trail which ends up with ToPrimitive converting the empty array into an empty string, which is then finally converted to0
by ToNumber. We can now substitute0
for each instance of+[]
:Simpler already. As for
++[[]][0]
, that’s a combination of the prefix increment operator (++
), an array literal defining an array with single element that is itself an empty array ([[]]
) and a property accessor ([0]
) called on the array defined by the array literal.So, we can simplify
[[]][0]
to just[]
and we have++[]
, right? In fact, this is not the case because evaluating++[]
throws an error, which may initially seem confusing. However, a little thought about the nature of++
makes this clear: it’s used to increment a variable (e.g.++i
) or an object property (e.g.++obj.count
). Not only does it evaluate to a value, it also stores that value somewhere. In the case of++[]
, it has nowhere to put the new value (whatever it may be) because there is no reference to an object property or variable to update. In spec terms, this is covered by the internal PutValue operation, which is called by the prefix increment operator.So then, what does
++[[]][0]
do? Well, by similar logic as+[]
, the inner array is converted to0
and this value is incremented by1
to give us a final value of1
. The value of property0
in the outer array is updated to1
and the whole expression evaluates to1
.This leaves us with
... which is a simple use of the addition operator. Both operands are first converted to primitives and if either primitive value is a string, string concatenation is performed, otherwise numeric addition is performed.
[0]
converts to"0"
, so string concatenation is used, producing"10"
.As a final aside, something that may not be immediately apparent is that overriding either one of the
toString()
orvalueOf()
methods ofArray.prototype
will change the result of the expression, because both are checked and used if present when converting an object into a primitive value. For example, the following... produces
"NaNfoo"
. Why this happens is left as an exercise for the reader...Let’s make it simple: