That’s it. That’s the meme.

    • Telemachus93@slrpnk.net
      link
      fedilink
      arrow-up
      14
      ·
      4 months ago

      Mh, ‘0’ is a nonempty string, so !‘0’ returns false. Then of course !(!‘0’) would return true. I’d absolutely expect this, Python does the same.

      And the second thing is just JavaScript’s type coercion shenanigans. In Python

      bool('0') # returns True because of nonempty string
      bool(int('0')) # returns False because 0 == False
      

      Knowing that JavaScript does a lot of implicit type conversions, stuff like that doesn’t strike me as very surprising.

  • jedibob5@lemmy.world
    link
    fedilink
    English
    arrow-up
    13
    ·
    4 months ago

    Javascript’s type coercion is rather insane, yes, but there is an actual, practical reason it’s done. JS, having been designed to be run in web browsers, wants to avoid blowing up and crashing at all costs. If it gets an unusual type comparison, usually the result of a bug, it tries to return something, such that the script can continue running if at all possible. In JS’ mentality, keeping a page running, even if it might not completely function properly, is preferable to throwing an unhandled exception and completely crashing it.

    Whether or not that is the right approach is debatable, but there is at least some logic to it. Personally, I think that the proliferation of Node letting JS run outside of browsers exacerbates a lot of JS’ issues, but TypeScript does a lot to make it look like a more sensible language.

    • Sleepless One@lemmy.mlOP
      link
      fedilink
      English
      arrow-up
      7
      ·
      4 months ago

      It’s from Javascript: The Definitive Guide 7th Edition by David Flanagan. It’s the O’Reilly book with the rhinoceros on it.

      • Midnitte@beehaw.org
        link
        fedilink
        English
        arrow-up
        9
        ·
        4 months ago

        It’s the O’Reilly book with the rhinoceros on it.

        Honestly O’Reilly should just remove the titles and leave just the animals to describe the books.

  • Semperverus@lemmy.world
    link
    fedilink
    English
    arrow-up
    6
    ·
    4 months ago

    This makes it make so much more sense…

    Its not really insanity, just a lot of hidden function calls

  • yetAnotherUser@lemmy.ca
    link
    fedilink
    arrow-up
    5
    ·
    4 months ago

    Hey OP, do you mind checking if your book explains the type coercions that are used with the + operator? I remember it also being mind-boggling, so I was hoping you book could demystify it too.

    • Sleepless One@lemmy.mlOP
      link
      fedilink
      English
      arrow-up
      3
      ·
      4 months ago

      I don’t recall if it covers that sadly. I read it months ago and this part stood out to me.

      • retrolasered@feddit.uk
        link
        fedilink
        English
        arrow-up
        6
        ·
        4 months ago

        I genuinely wasn’t aware of that. I must be getting javascript confused for almost any other language. I wonder how many times ive !!'d a value to make that work without actually absorbing that into my head now…

        • Telemachus93@slrpnk.net
          link
          fedilink
          arrow-up
          3
          ·
          4 months ago

          In other languages that shouldn’t be equal either though, right?

          Maybe you meant

          if (2){
          console.log("nonzero ints are truthy")
          }
          else {
          console.log("no they're not")
          }
          

          Which would output

          nonzero ints are truthy
          

          and that would actually work in all languages I know. But that’s different from being equal.

  • kureta@lemmy.ml
    link
    fedilink
    arrow-up
    2
    ·
    4 months ago

    why convert boolean to integer instead of converting the other operand to boolean? it doesn’t make sense.