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”

  • /null/dev
    link
    fedilink
    English
    1442 years ago

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

    • Deebster
      link
      fedilink
      English
      40
      edit-2
      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
      link
      fedilink
      English
      232 years ago

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

    • @[email protected]
      link
      fedilink
      English
      13
      edit-2
      2 years 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]
    link
    fedilink
    English
    192 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]
    link
    fedilink
    English
    82 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

  • @flurry
    link
    English
    52 years ago

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

  • Haus
    link
    fedilink
    42 years ago

    That reads remarkably like a Whitehead theorem in Principia Mathematica.

  • @[email protected]
    link
    fedilink
    English
    32 years ago

    For the lazy, but curious, of us…

    (! []+[]) [! + [
    ]+ !+[]]+([
    ]+{})[ + ! ! [ ]
    ]+( ! [ ]+[ ] ) [
    !+[]+ !+[]]
    
  • lapes
    link
    fedilink
    22 years ago

    I always thought Python was more popular

  • @weird_nugget
    link
    English
    12 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.