Why and how does ([![]]+[][[]])[+!+[]+[+[]]] evaluate to the letter “i”? [duplicate]

折月煮酒 提交于 2019-11-26 22:06:37
Blender

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:

  • The string ([![]]+[][[]]):
    • [![]] is [false].
    • [][[]] is undefined.
    • Add them together and you get "falseundefined".
  • And the index: [+!+[]+[+[]]]. Some whitespace and parentheses will make the operations much clearer: [+(!(+[])) + [+[]]]:
    • [+[]] is [0].
    • +[] coerces [] to an integer, so you get 0.
    • !+[] coerces 0 to a boolean and negates it, so you get true.
    • +!+[] coerces true to an integer, so you get 1.
    • Add them together, and you get ["10"].

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.

![] returns false. Then we use [...] to get the .toString() behavior of +. ([]+[] is the same as [].toString()+[].toString()) the [][[]] is undefined because we're trying to access the index [] (or [].toString(), which is '') of [] which is undefined.

Sorry for the previous answered, I misread your comment totally.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!