this post was submitted on 19 Jul 2023
22 points (100.0% liked)

Programmer Humor

23230 readers
1183 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"

top 9 comments
sorted by: hot top controversial new old
[–] [email protected] 15 points 2 years ago (2 children)

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

[–] [email protected] 3 points 2 years ago

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

[–] [email protected] 2 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"-
[–] [email protected] 5 points 2 years ago

Javascript might be the most widely-used scripting language in use today, due to its browser dominance. Most popular would imply that it's not completely despised by everyone that has to use it, which is misleading. Even TypeScript tutorials are about 50% 'you have to understand what Javascript does wrong here'.

[–] [email protected] 1 points 2 years ago

That reads remarkably like a Whitehead theorem in Principia Mathematica.

[–] [email protected] 1 points 2 years ago

Good reminder that no matter how deep in your programming career you are, there will always be a JS snippet you won't be able to understand.

[–] [email protected] 1 points 2 years ago
[–] [email protected] 1 points 2 years ago

"Today we're going to write our first program, and all it will do is print 'hello world!'"

Everyone: oh, cool!

JS learners: fml

[–] [email protected] 1 points 2 years ago