Why (([[] === []) + / - /) [1] = 'a' and (1 + {}) [(1 << 1) +1] = 'b' in javascript?

I recently came across an interesting website that illustrates Javascript Obfuscator: http://bl.ocks.org/jasonsperske/5400283

For example, (([]===[])+/-/)[1] gives a and (1+{})[(1<<1)+1] gives b .

I tried to understand the sequence of evaluating these confusing results, but was in vain.

Taking (1+{})[(1<<1)+1] as an example, I understand that << is a bitwise shift operator and returns 2, so the expression becomes (1+{})[3] . But then I can’t understand what this means 1+{} and [3] .

Google is not very useful for this problem, because search engines do not really like brackets or slashes, so if there are duplicate questions, I regret it.

+5
source share
3 answers

These are just obfuscation tricks.

eg:

[]===[] ===> false

and

([]===[])+/-/ ===> "false/-/" (you can test it in the console yourself)

So what is (([]===[])+/-/)[1] ? (second char)

That's right: 'a'

You can also look at this:

enter image description here

+5
source

1+{} result is the string "1[object Object]" , (1+{})[3] is to get a char of index 3, which is equal to b .

First example:

[]===[] Comparison of two different objects with === , so the result is false , the toString result of which is "false" .

/-/ is a regular expression object whose toString result is "/-/"

When you do false + /-/ which will use the result .toString() , so the result will be "false/-/" and the second char is a .

+3
source

You can go step by step:

 (([]===[])) 

just false . Converted to the string "false/-/" and indexed using [1] , gives you a string "false".

The same applies to (1+{}) , which results in the string "1[object Object]" . And 1 <1 + 1 is another way of writing 3 , so this results in "1[object Object]"[3] , which is simply equal to b .

+3
source

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


All Articles