Javascript is the most popular scripting language in use today

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"

361 points · 22 comments · view on lemmy.world

22 Comments

nulldev@programming.dev · 144 pts · 3y (5 replies)

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

Deebster@lemmyrs.org · 40 pts · 3y

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 · 23 pts · 3y

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

arandomthought@sh.itjust.works · 13 pts · 3y

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

elvith@feddit.de · 7 pts · 3y (1 reply)

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

PipedLinkBot@feddit.rocks · 7 pts · 3y

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.

addie@feddit.uk · 19 pts · 3y (1 reply)

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

ngdev@lemmy.world · 6 pts · 3y

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

ADandHD@lemmy.sdf.org · 15 pts · 3y

.... I hate this....

gravitas_deficiency@sh.itjust.works · 10 pts · 3y (1 reply)
Von_Broheim@programming.dev · 4 pts · 3y

A classic

red@feddit.de · 8 pts · 3y

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

Everyone: oh, cool!

JS learners: fml

_Z1useri@sopuli.xyz · 7 pts · 3y
julianh@lemm.ee · 6 pts · 3y

Don't forget jsfuck

flurry@lemmy.world · 5 pts · 3y

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

Haus@kbin.social · 4 pts · 3y

That reads remarkably like a Whitehead theorem in Principia Mathematica.

ZodiacPi@programming.dev · 3 pts · 3y

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

(! []+[]) [! + [
]+ !+[]]+([
]+{})[ + ! ! [ ]
]+( ! [ ]+[ ] ) [
!+[]+ !+[]]
lapes@kbin.social · 2 pts · 3y

I always thought Python was more popular

weird_nugget@lemmy.world · 1 pts · 3y

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.

abbadon420@lemm.ee · 1 pts · 3y

Classic javascript