Live data from Hacker News

JSFuck – esoteric JavaScript

jsfuck.com

31–39 of 39 posts

Re: JSFuck – esoteric JavaScript

#31
post #30
post #10

I know this will most likely come as a surprise to most Javascript programmers but all code is represented using only two "characters" deep inside your little computers!

What do you mean two characters? What are they exactly? I'm guessing $ and ; because it's like jQuery but some operations seem impossible, like how does it even assign variables?

Its quite simple. The "$" and ";" are used in groups of three to signify other characters.

    "$$$" = "["
    "$$;" = "]"
    "$;$" = "("
    "$;;" = ")"
    ";$$" = "+"
    ";$;" = "!"

Re: JSFuck – esoteric JavaScript

#32
I seem to remember CloudFlare using this approach (for obfuscation purposes) when users enable the 'under attack' mode on their sites. I was pretty surprised something like this was possible when I first saw the code.

Re: JSFuck – esoteric JavaScript

#33

For those wondering how you get numbers, strings and other primitives from a whole bunch of empty arrays and objects in JavaScript, here's what happens when you do arithmetic and other operations on arrays and objects: > [] + [] "" (An empty string) > {} + [] 0 (The number zero) > [] + {} "[object Object]" (.toString() called on a plain object). > ![] false (!{} is the same) > !![] true (not false) > +[] 0 (the numbe…

Here's a slightly more entertaining version of the discrepancies/oddities found in javascript:

https://www.destroyallsoftware.com/talks/wat

Re: JSFuck – esoteric JavaScript

#34

Earlier quoted context omitted.

Does knowing any of this make you a better programmer? I'd say no. Should you be using any of this in production code? No The next programmer even if he is a really good one might not know that particular esoteric trick. I'm not trying to vote down or anything, but what is the point? Most of it look like language design warts to me. Most of them should have thrown exceptions and errored out. Now I'm loving the idea o…

Useful for obfuscating executable JS for security reasons.

Security through obscurity is not security. This would be easy to translate back into the original code anyway. This is also not efficient from a file size perspective which is cancer on the web.

Re: JSFuck – esoteric JavaScript

#35
post #30
post #10

I know this will most likely come as a surprise to most Javascript programmers but all code is represented using only two "characters" deep inside your little computers!

What do you mean two characters? What are they exactly? I'm guessing $ and ; because it's like jQuery but some operations seem impossible, like how does it even assign variables?

I think he meant 1 and 0

Re: JSFuck – esoteric JavaScript

#36

For those wondering how you get numbers, strings and other primitives from a whole bunch of empty arrays and objects in JavaScript, here's what happens when you do arithmetic and other operations on arrays and objects: > [] + [] "" (An empty string) > {} + [] 0 (The number zero) > [] + {} "[object Object]" (.toString() called on a plain object). > ![] false (!{} is the same) > !![] true (not false) > +[] 0 (the numbe…

https://www.destroyallsoftware.com/talks/wat

Re: JSFuck – esoteric JavaScript

#37

For those wondering how you get numbers, strings and other primitives from a whole bunch of empty arrays and objects in JavaScript, here's what happens when you do arithmetic and other operations on arrays and objects: > [] + [] "" (An empty string) > {} + [] 0 (The number zero) > [] + {} "[object Object]" (.toString() called on a plain object). > ![] false (!{} is the same) > !![] true (not false) > +[] 0 (the numbe…

Does knowing any of this make you a better programmer? I'd say no. Should you be using any of this in production code? No The next programmer even if he is a really good one might not know that particular esoteric trick. I'm not trying to vote down or anything, but what is the point? Most of it look like language design warts to me. Most of them should have thrown exceptions and errored out. Now I'm loving the idea o…

Using +x to convert things to numbers and x|0 to convert things to 32-bit integers are useful and common operations.

Re: JSFuck – esoteric JavaScript

#38

For those wondering how you get numbers, strings and other primitives from a whole bunch of empty arrays and objects in JavaScript, here's what happens when you do arithmetic and other operations on arrays and objects: > [] + [] "" (An empty string) > {} + [] 0 (The number zero) > [] + {} "[object Object]" (.toString() called on a plain object). > ![] false (!{} is the same) > !![] true (not false) > +[] 0 (the numbe…

A bunch of that is wrong or at least misleading.

> {} + [] > 0 (The number zero)

This is wrong, it is only true in the weird scenario that you type it into the jsconsole and don't use the result. It parses as an empty block ({}) and then an unrelated unary +[] after that. var x = {} + []; gives you a string and it is the same as var x = [] + {}.

There is more logic than it seems here:

  Unary plus:
  > always gives a number (a double: NaN is a double)
  > on array: +a -> if len>=2: NaN else +a[0] (recursively)
  > on object: NaN
  > on string: parseNumber, or NaN if that fails
  > on bool: 0 for false, 1 for true
  > on null: 0
  > on undefined: NaN

  binary plus (addition):
  > order doesn't matter except for the jsconsole pseudo-bug
  > if both sides are number or boolean, add them
  > else, toString() both sides and concat them

  boolean negate (not):
  > false for 0, false, "", null, undefined
  > true otherwise

  binary - (subtraction)
  > x-y coerces x and y to number and subtracts them.
  > Most mainstream languages that use + for string concat don't use - for some sort of string unconcat so its not too crazy.

  > typeof [] === typeof {}

  This isn't so crazy, typeof any non-primitive gives you "object".
  [] is pretty much just an object plus magic .length property,
  var x = []; x.blah = 7; console.log(x.blah) works. It doesn't work on number or bool.

Re: JSFuck – esoteric JavaScript

#39
post #30

Earlier quoted context omitted.

What do you mean two characters? What are they exactly? I'm guessing $ and ; because it's like jQuery but some operations seem impossible, like how does it even assign variables?

I think he meant 1 and 0

yes
Post reply on HN