this post was submitted on 19 Jul 2023
321 points (97.3% liked)

Programmer Humor

25773 readers
1426 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 

Explanation: it's mostly due to how js does type conversion. for the Ls, it's

[] is an empty array ![] is treated as false combining a boolean with the empty array returns "false" as a string (so true + [] = "true", false + [] = "false") ! + [] is treated as true ! + [] + ! + [] is treated as 2 since true + true = 1 + 1 = 2 so you have "false"[2], which is l for the o it's [] is an empty array [] + {} returns "[object Object]" as a string ({} + [] returns 0) ![] is false !![] is true +!![] casts it to an integer so that part is "[object Object]"[1], which returns "o"

you are viewing a single comment's thread
view the rest of the comments
[–] nulldev@programming.dev 136 points 2 years ago (3 children)

The explanation is about as understandable as the JS code, however thanks OP, TIL

[–] Deebster@lemmyrs.org 37 points 2 years ago* (last edited 2 years ago)

The line breaks haven't worked, here's it formatted correctly:

Explanation: it's mostly due to how js does type conversion.

For the Ls, it's:

  • [] is an empty array
  • ![] is treated as false
  • combining a boolean with the empty array returns "false" as a string (so true + [] = "true", false + [] = "false")
  • ! + [] is treated as true
  • ! + [] + ! + [] is treated as 2 since true + true = 1 + 1 = 2
  • so you have "false"[2], which is l

for the o it's:

  • [] is an empty array
  • [] + {} returns "[object Object]" as a string ({} + [] returns 0)
  • ![] is false
  • !![] is true
  • +!![] casts it to an integer
  • so that part is "[object Object]"[1], which returns "o"-
[–] Sonotsugipaa@lemmy.dbzer0.com 21 points 2 years ago

Iirc this is called "JSFuck", and it has a number of compilers

[–] elvith@feddit.de 6 points 2 years ago (1 children)

There's a nice presentation that explains this behaviour quite understandable: https://youtu.be/sRWE5tnaxlI

[–] PipedLinkBot@feddit.rocks 7 points 2 years ago

Here is an alternative Piped link(s): https://piped.video/sRWE5tnaxlI

Piped is a privacy-respecting open-source alternative frontend to YouTube.

I'm open-source, check me out at GitHub.