Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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

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

I have tried hard to understand the evaluation sequence of these obfuscated result but was in vain.

Taking (1+{})[(1<<1)+1] as an example, I understand that << is the bitwise shift operator and will return 2, so the expression becomes (1+{})[3]. But then I cannot understand what does it mean by 1+{} and [3].

Google isn't really helpful to this problem as search engines don't like the brackets or slashes very much, so in case there are duplicate questions I'm sorry about that.

like image 256
Pingu Avatar asked Aug 25 '14 08:08

Pingu


People also ask

What does << do in JavaScript?

Description. This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.

What is the difference between >> and >>> operators in JavaScript explain?

The >>> operator is identical to the >> operator, except that the bits that fill in the shifted left bits have the value of 0. The >>> operator is said to be an unsigned shift because it does not preserve the sign of the operand.

What does && and || mean in JavaScript?

The logical and ( && ) and or ( || ) are logical operators in JavaScript. Normally, you're using these operators on booleans: true && true // => true. true && false // => false.

What is the use of << left shift operator?

The left-shift operator causes the bits in shift-expression to be shifted to the left by the number of positions specified by additive-expression . The bit positions that have been vacated by the shift operation are zero-filled.


2 Answers

It's just obfuscation tricks.

for example :

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

and

([]===[])+/-/ ===> "false/-/" ( You could test it in the console by yourself)

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

That's right :'a'

You may want to look at this also :

enter image description here

like image 146
Royi Namir Avatar answered Sep 27 '22 23:09

Royi Namir


You could go step by step:

(([]===[]))

is simply false. Converted into a string "false/-/"and indexed by [1] gives you the a of the string "false".

The same goes for (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 b.

like image 32
Thomas Junk Avatar answered Sep 28 '22 00:09

Thomas Junk