Live data from Hacker News

JSFuck – esoteric JavaScript

jsfuck.com

1–10 of 39 posts

Re: JSFuck – esoteric JavaScript

#4

with eval checked, alert(1) will produce 1227 chars while alert(1); will produce 9535 chars. Interesting.

Well ; produces 8307 chars on its own (without eval checked) -- which seems kind of inefficient (for such a common character in idiomatic javascript). Looking that their encoder, ';' actually has a specific encoding (which itself has to be encoded) so it looks like there's some inefficient expansion taking place (e.g. '.' has a specified encoding that does not require recursive encoding). Encoding the string "link" in the expansion of ';' appears to be very expensive -- alert is cheap in comparison because you can obtain its letters from Javascript return values (e.g. "l" is pulled out of "false" which is obtained by (![]+"")[2])

I'd imagine that if you were serious about this, you'd implement, say, e=String.fromCharCode (12k chars) and use that to dig yourself out of a lot of this expensive stuff if you need more than one hard-to-encode character.

Re: JSFuck – esoteric JavaScript

#9
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 number zero)

> -[]

-0 (negative zero)

> +{}

NaN (Not a number, same goes for -{})

> "" + []

"" (empty string)

> "" - []

0 (number zero, not empty string)

And the one that gets more people than the previous list:

> typeof [] === typeof {}

true

Incidentally, some of these things can be useful. For example, +(x) will always evaluate to a double (unless it is preceded by a string), while (x|0) will always evaluate to a 32bit integer. asm.js abuses (to an extent) this to have more control over types, and most JavaScript engines would now store the result of (x|0) as an integer internally instead of a double.

Post reply on HN