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: 
In JS:

* [] is an empty array
* ! means "not" in js, so if the value after it is true, then it
becomes false and vice versa
* "+" in front of a boolean in js is a unary plus. It converts the
value to an integer. [How do I convert a string into an integer in JavaScript?](https://stackoverflow.com/questions/1133770/how-do-i-convert-a-string-into-an-integer-in-javascript)

The JS elements:

```
+[]: 0
![]: true
+![]: 1
!![]: false
+!![]: 0

```

In PHP:

* array() is an empty array
* ! means "not", so it converts to true to false and vice versa as in js
* (int) will typecast the boolean to an integer, like the + did in js.

The PHP Elements:

```
(int) empty(array()): 0
empty(array()): true
(int) empty(array()): 1
!empty(array()): false
(int) !empty(array()): 0

```

Now add it all up. Good luck.
```
+[] -> 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

```