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”

144 points

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

permalink
report
reply
40 points
*

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”-
permalink
report
parent
reply
23 points

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

permalink
report
parent
reply
13 points
*

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”.

permalink
report
parent
reply
7 points

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

permalink
report
parent
reply
7 points

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.

permalink
report
parent
reply
19 points

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’.

permalink
report
reply
6 points

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

permalink
report
parent
reply
15 points

… I hate this…

permalink
report
reply
10 points
permalink
report
reply
4 points

A classic

permalink
report
parent
reply
8 points

“Today we’re going to write our first program, and all it will do is print ‘hello world!’”

Everyone: oh, cool!

JS learners: fml

permalink
report
reply

Programmer Humor

!programmer_humor@programming.dev

Create post

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

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics

Community stats

  • 2.9K

    Monthly active users

  • 1K

    Posts

  • 38K

    Comments