Question: Can anybody explain me why this javascript code alerts "47" ?

```
var component = +((!+[] + !![] + !![] + !![] + []) + (!+[] + !![] + !![] + !![] + !![] + !![] + !![]));
alert(component);

```

how those brackets, plus signs, etc return 47 at the end when I execute them ?

how to write this in PHP. I understand this is something with strings, booleans, etc.. but can't understand it ?

Answer: 
```
false       =>  ![] 

true        =>  !![] 

undefined    =>  [][[]] 

NaN          =>  +[![]] 

0            =>  +[] 

1            =>  +!+[] 

2            =>  !+[]+!+[] 

10           =>  [+!+[]]+[+[]] 

Array        =>  [] 

Number      =>  +[] 

String       =>  []+[] 

Boolean      =>  ![] 

Function     =>  []["filter"] 

eval         =>  []["filter"]["constructor"]( CODE )()

window      =>  []["filter"]["constructor"]("return this")()

```

Just about anything javascript can be turned into incredibly hard to read crap if you use those conversions. It is stupid, but it can be done.

So take the 10 for example:

```
[+!+[]]+[+[]] 

```

This is really two components:

```
[+!+[]]

and

[+[]] 

```

This is just `[1]` and `[0]`. But if we add them together, they are cast to strings, so we get "1" + "0" which gives us "10".

For your question:

```
+((!+[] + !![] + !![] + !![] + []) + (!+[] + !![] + !![] + !![] + !![] + !![] + !![]));

```

I'll break down the 4 component:

```
(!+[] + !![] + !![] + !![] + [])

true + true + true + true + array

```

which if you run it in the console, gives you 4. The last [] does nothing for this part, but it forces the typecast to string, which is important for the next part.

Then, the next one becomes

```
true + true + true + true + true + true + true

```

which gives you 7.

So you get 4 + [] + 7, which makes "47" since the `+ [] +` forces the typecast to string.

To do it in PHP, you could do something like:

```
$x = (![] + ![] + ![] + ![]) . (![] + ![] + ![] + ![] + ![] + ![] + ![]);
//$x is now 47

```
```
+[] -> 0 //Because + converts all other javascript types to numbers
!+[] -> true //Because ! converts 0 to true
![] -> false //Empty array is true 
!![] -> true //Because ![] is false

```

`(!+[] + !![] + !![] + !![] + []) -> true+true+true+true+""="4"`

`+` converts **Boolean** value to **1** if it is `true`, and **0** if it is `false`.` 

And **this is a string value**,because `Number`+`Empty object` creates a **string**, because an **Empty Object** can't convert to `Number` it converts to a `String`. And as we know `Number+String->String`. 

**As a result:** `(!+[] + !![] + !![] + !![] + [])` **is the same as** `"4"`

In the same way

```
(!+[] + !![] + !![] + !![] + !![] + !![] + !![]) -> true+true+true+true+true+true+true = 7 //this is a Number, because Boolean value converts to a Number: either 0 or 1    

```

And finally 

```
"4"+7=47

```