The following code apparently prints the string
This involves implicit type casting and I'm trying to understand how exactly this line is interpreted.
I've isolated each character
I've also managed to break down the expressions returning each letter apart from
! an empty array is an Object, which according to ECMAScript documentation, point 9.2 evaluates to
true when converted to a
boolean so this is
false+ as per Point 11.6.1 both arguments of the binary
+ operator get converted to String, therefore we get
"false"+"", which evaluates
+ a unary plus operator causes a
ToNumber conversion followed by a
ToPrimitive conversion if the argument is an
Object. The result of such conversion is determined by calling the
[[DefaultValue]] internal method of the object. In case of an empty array, it defaults to
(ECMAScript Documentation, sections: 11.4.6, 9.3, 9.1 )
"false" we're accessing the character at index
0, hence the
Same story, the only difference here are additional conversions in the part in square brackets (which evaluates to a number to point at another character in the string
"false"), triggered by the use of unary
+ evaluates to
0, as explained above.
"false" returns the second character in the string, which is
!+ evaluates to
true as explained above
true+true using the binary
+ on primitives triggers a
ToNumber conversion. In case of true, its result is
"false" - self explanatory
What leaves me stumped is the letter
"i". I can see that the second part (in square brackets) evaluates to the string
"10" and that the first part (in parentheses) returns
"falseundefined" but I can't make heads or tails of how this is happening. Could someone explain it step by step? Especially the magic that happens with square brackets? (arrays and array access)
If possible, I'd like each step to contain a link to the underlying ECMAScript rules.
What I find the most cryptic is this part:
Your cryptic part isn't all that cryptic if you rewrite it a little:
 will be coerced into a string because it isn't an integer, so you're looking for a property of
 with the name
'' (an empty string). You'll just get
undefined, as there is no property with that name.
As for the actual letter, break the expression up into the two main components:
[+!++[+]]. Some whitespace and parentheses will make the operations much clearer:
[+(!(+)) + [+]]:
to an integer, so you get
0to a boolean and negates it, so you get
trueto an integer, so you get
When using a string to access the properties of the array and the string happens to be an element of the array, the string is coerced into an integer and you get back the actual element of the array:
> [1, 2, 3]["0"] 1 > [1, 2, 3]["1"] 2
So your final result is:
> "falseundefined"["10"] "i"
Read this answer for an explanation of the
[false] + undefined part.
([!]+[])[+!++[+]] has two parts :
([!]+[]) and the other which you found yourself.
false. Then we use
[...] to get the
.toString() behavior of
+ is the same as
[] is undefined because we're trying to access the index
.toString(), which is
 which is undefined.
Sorry for the previous answered, I misread your comment totally.
©2020 All rights reserved.