Advertisement
Guest User

Untitled

a guest
Aug 8th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. []               // This is an array literal.
  2. [][123]          // This gets item 123 of the array. That value is undefined (=== void 0).
  3. []['hello']      // This boxes the array type to array object (=== new Array()) and tries
  4.                  // to read a property 'hello', from it. Undefined again.
  5. [][[]]           // This treats [] as an accessor. It will try to cast it, if it casts it
  6.                  // as number or string doesn't matter, the result is that whatever property
  7.                  // it tries to find is undefined.
  8. [1,2,3] + ' go!' // === '1,2,3 go!'; array is cast to string to allow adding it to a string,
  9.                  // becoming [1,2,3].join(',') (and this is why you can console.log() anything,
  10.                  // things have a default .toString() implementation).
  11. [][[]]+[]        // This tries to add undefined and empty array. Hmm... can't add those types.
  12.                  // So convert them to something add-able.
  13.                  // [].join(',') becomes ''. What is the textual conversion of undefined?
  14.                  // 'undefined'!
  15. +[]              // Positive number... Array?? Array is not a number, sir. Hm, [].join(',')
  16.                  // evaluates to ''. If it was [123].join(','), we could return 123 as the value...
  17.                  // But now we have to convert '' to number. An empty string evaluates to false
  18.                  // in an if() block, right? And everyone knows false is a bunch of zeros.
  19.                  // So +[] is positive zero! (-[] would be === -0, negative zero, but ancient
  20.                  // mathematicians would not accept that result).
  21. 'undefined'[0]   // Yay, if we treat the string like an array, it's an array of characters!
  22.                  // So this is === 'u'. And that looks like a happy emoticon so it must be a
  23.                  // good thing.
  24. [][[]]+[]        // ...so if this is the string 'undefined'...
  25. +[]              // ...and this is zero...
  26. [][[]]+[][+[]]   // ...dang! Syntax error. Operator precedence is not helping...
  27. ([][[]]+[])[+[]] // ...aha! Parens can tell the right order to apply the operators and therefore
  28.                  // how to implicitly cast the types. We can add that character with other
  29.                  // characters with +, and make a string! Hooray!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement