Who will explain this amazing string of Javascript code?

  node.js, question

Code above:

(!  (~ plus []) plus {}) [-[~ plus ""] [plus []]] * [~ plus []] plus ~ ~!  Plus []] plus ({} plus []) [[[~!  Add []] * ~ add []

Can be used in any Javascript environment. Output method:

 (!  (~ plus []) plus {}) [-[~ plus ""] [plus []]] * [~ plus []] plus ~ ~!  Plus []] plus ({} plus []) [[[~!  Add []] * ~ add []

Just looked at the next sentence by sentence of node, it is actually various types of implicit conversion:
Let’s split the above code into two parts:
(! (~ plus []) plus {}) [-[~ plus ""] [plus []]] * [~ plus []] plus ~ ~! Add []]And({} plus []) [[[~! Add []] \ * ~ add []

For the previous string of codes(! (~ plus []) plus {}) [-[~ plus ""] [plus []]] * [~ plus []] plus ~ ~! Add []]
Add []Add a sign in front of the empty Array declared in square brackets to implicitly convert the array type to Number type, and the Number corresponding to the empty array is 0.
~ plus []Add a ~ sign before 0 to indicate inversion by position, refer to here.Javascript bitwise inversion operator (~)The result should be -1.
! (~ plus [])Add before -1! Number is implicitly converted to Boolean. The Boolean type corresponding to -1 is, of course, false.
! (~ plus []) plus {}The purpose of this investigation is to automatically call the toString () method of the two operands when both operands before and after the addition operator are not of type Number or String, and convert them to type string for splicing. So output hereFalse [objectundefined]/note, here is the string

(! (~ plus []) plus {})[/* index */]This is to take the value of the previous String, because we know that the string in JS can obtain a certain character through an array-like method. For examplevar str = "Hello World"; var char = str[4]; //array subscript starts with 0. Then let’s take a look at which character it takes.

-[~ plus ""] [plus []] * [~ plus []] plus ~ ~! Add []The final value of the code on the left is the index it takes. Then let’s start to analyze what this value is:
-[~ plus ""] [plus []]According to the previous method, starting from the innermost part, a “”represents an empty string, preceded by an addition sign, which is implicitly converted to the Number type, where the converted value is 0, followed by a ~ sign, which was introduced earlier. this is inverted by bit, and inverted by bit, followed by brackets~ add ""The value is -1, followed by,[-1]It means a indexed array with an element whose value is -1. Then[-1][ plus []]From the above experience we know that this code is equivalent to[-1][0]So it finally gets the first element of the array, which is -1. Finally,-- -1This ..
Operation means that the current value is reduced by 1 before the subsequent operation, so the result of the current code block is -2.

Change the line, slow down.

-[~ plus ""] [plus []] * [~ plus []]Here, I took one at the back.[~ plus []]From the above, we can know that this is a ride[-1], then multiplied by an array how to do, in this case, it is also implicitly converted, just this array has only one element, then converted to Number is to take its first element to Number type. The final value of this little piece of code is 2.
This code is followed by this paragraph~~! Add []According to the above analysis method,Add []Is 0! To cast to Boolean, 0 to boolean is false, ~ to invert by bit, for a number, inverting by bit twice equals itself. Here~Converts Boolean type to Number type, false corresponds to Number type value of 0, so~~! zeroThe result is 1.

By sorting out the above results,-[~ plus ""] [plus []] * [~ plus []] plus ~ ~! Add []The value of is 3. The final thing is to takeFalse [objectundefined]/here is a stringThe third character of iss. (write so much to get an s. . . )

In the same analysis method as the following,{} plus []Then the object and array are converted to String type for addition, and the empty array is an empty String after being converted to string, so the value after the above code conversion is[objectobject]/here is a string
The following code is to obtain a character of this string.[~! Add []] * ~ add [], the execution result of this string of codes is the subscript value of the character to be obtained.[~! Add []]The operation value of is[-2] // is an arrayThis array is multiplied by~ plus [](the result of the operation is -1), the two operands are implicitly converted to Number type, and the multiplication result is 2.

Finally, the latter half of the code is obtained[objectobject]/here is a stringThe second character b of this string.

It is estimated that you know all the final output. Personally, this magic code is a good example of knowledge about javascript implicit type conversion. This also goes around. . .