this post was submitted on 19 Jul 2023
344 points (97.5% liked)

Programmer Humor

19503 readers
1264 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 1 year 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 20 comments
sorted by: hot top controversial new old
[–] [email protected] 144 points 1 year ago (4 children)

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

[–] [email protected] 40 points 1 year ago* (last edited 1 year 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] 23 points 1 year ago

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

[–] [email protected] 13 points 1 year ago* (last edited 1 year ago)

The idea behind it is not that complicated to explain... They set up a construct that spits out the word "false" and one that spits out "[object Object]". Taking the character with the Index of 2 from "false" gives us the "l" (the "2" to use as the index is of course also created using only brackets and parentheses). The character with the index of 1 from "[object Object]" is "o". So we have everything we need to spell out "lol".

[–] [email protected] 7 points 1 year ago (1 children)

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

[–] [email protected] 7 points 1 year 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.

[–] [email protected] 19 points 1 year ago (1 children)

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] 6 points 1 year ago

I think they're using definition three from Merriam-Webster

[–] [email protected] 15 points 1 year ago

.... I hate this....

[–] [email protected] 10 points 1 year ago (1 children)
[–] [email protected] 4 points 1 year ago
[–] [email protected] 8 points 1 year 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] 7 points 1 year ago
[–] [email protected] 6 points 1 year ago
[–] [email protected] 4 points 1 year ago

That’s smart ! Better than the classic ‘baNaNa’

[–] [email protected] 4 points 1 year ago

That reads remarkably like a Whitehead theorem in Principia Mathematica.

[–] [email protected] 3 points 1 year ago

For the lazy, but curious, of us...

(! []+[]) [! + [
]+ !+[]]+([
]+{})[ + ! ! [ ]
]+( ! [ ]+[ ] ) [
!+[]+ !+[]]
[–] [email protected] 2 points 1 year ago

I always thought Python was more popular

[–] [email protected] 1 points 1 year 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 1 year ago

Classic javascript

load more comments
view more: next ›