Skip to content

Instantly share code, notes, and snippets.

View codemaster138's full-sized avatar

Jake codemaster138

  • Germany
  • 17:03 (UTC +02:00)
View GitHub Profile
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDCXqRT2n+dk1ownCgvjpN7SRcFYtpZn6aAjY3/HciNWdBklaEl96YfH7NAim8tTwM+8l2ZKxRILy2NEHKYxg65FxPzT/mGnuqpJthn1qYCiKybfRTtm1Yveoy7+GA3Y/TmlLqUxufbdU4WY0inFhafnufJBkPMpG/FWsf7mDHkv0l/ARmG/6y8kqy4bCncxciVVOa8qkPwxDnw8/f1ED3TE4S6R8cbqdvuEdj1HJjA9Gxa59naPwvFDkx4pWSo+a06R708V4FsAXyAgtmw1ZMelftgUoTn1QfIa1UoEd7YGyGTrjU0rlwXHV0GW6rw8nslAqC8AHsQDDc9PN1+ojjNY0R6BI0b12vhptItRk90keS0336JNRHmaBwPGUvN+EMtbXa8Hm3RzWI/S5PtDgPhjec/E/FYa1kUPF6vYmjSIK3bwERkmjXBagG7FzZbolwEfbhH/TTgCO+4n0QNdsAXIQNFBNdjUXbqu8YPAHcxwRnscm8Mir1sOsxDQERraq8= jake@alx
codemaster138 /
Last active October 4, 2022 20:31
Encode any number as a strange javascript array expression

I was recently browsing twitter when I found an interesting tweet claiming that "Any JS integer can be represented using only [, ] and +". The tweet also showed the number 10 encoded as ++[+[]][+[]]+[+[]]:

console.log( ++[+[]][+[]]+[+[]] ); // Logs '10'

This tweet intrigued me. Can really any javascript integer be represented in this way? I had to find out.

First, I tried to deconstruct the example value. The expression can be split into two main parts: ++[+[]][+[]] and [+[]], connected by a + addition operator. Running each individual expression in a javascript REPL showed that the first expression encodes the number 1 and the second the number 0. For some strange reason that is beyond me, javascript decides that because of the way the expression is written, the two numbers should be treated as strings and concatenated instead of being added (Why, javascript, why!?).