• guy@lemmy.world
    link
    fedilink
    arrow-up
    36
    ·
    11 months ago

    == is a heathen with no rightful place except equality to null. All praise ===

        • Limitless_screaming@kbin.social
          link
          fedilink
          arrow-up
          5
          ·
          11 months ago

          If " " wasn’t equal to 0, it wouldn’t make sense, but since a string containing a space equals 0, you’d expect the same to apply to a string containing a tab or a newline. (or at least I’d expect that)

          • FaceDeer@kbin.social
            link
            fedilink
            arrow-up
            2
            ·
            11 months ago

            I admit I have never dabbled in javascript, despite being a proficient programmer. I now dread to ask… would any string that contains only whitespace == 0? " \t\n \t " for example?

            • Limitless_screaming@kbin.social
              link
              fedilink
              arrow-up
              2
              ·
              11 months ago

              Yes, it would. Just like a string of spaces " " == 0, but it isn’t that bad; === is Javascript’s version of == in other languages, and, thus, you should be using it if you don’t want that wonkiness.

              == is just for convenience, like when you want to make sure that the user didn’t leave the form empty and the button shouldn’t be greyed out, and other UI stuff. Without these kinds of features JS wouldn’t be used in so many toolkits.

              • atx_aquarian@lemmy.world
                link
                fedilink
                arrow-up
                0
                ·
                11 months ago

                Ok, I always mistakenly assumed === was the identity operator in JS, too. TIL, thanks! As much as we like to poke fun at JS, every time I’m taught the rationale behind some aspect of it, I find it redeeming and even a little endearing.

                • bitcrafter@programming.dev
                  link
                  fedilink
                  English
                  arrow-up
                  2
                  ·
                  11 months ago

                  The explanation given to you makes it sound like == was deliberately designed to be a more convenient version of ===, but what actually happened was that == used to be the only equality operator in JavaScript, which meant that if you didn’t want it’s auto-coercing behavior then you needed to go out of your way to add additional type checks yourself. Because this was obviously a tremendously inconvenient state of affairs, the === operator was introduced later so that you could test for equality without having to worry about JavaScript doing something clever underneath the hood that you weren’t expecting.

      • sbv@sh.itjust.works
        link
        fedilink
        English
        arrow-up
        6
        ·
        11 months ago

        It’s a slash-t in the comment. Maybe kbin has different rendering rules for comments?

        • Limitless_screaming@kbin.social
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          11 months ago

          That would be weird if a string containing a space wasn’t equal to 0 " " == 0, but that’s not the case in JS. If you think that "" and " " being equal to 0 is weird then I agree, but since they are, you should expect "\t" and "\n" to equal 0 too.

    • jtk@lemmy.sdf.org
      link
      fedilink
      English
      arrow-up
      2
      ·
      11 months ago

      Yeah, it’s true. I knew all the other ones, had to put that one in the dev tools console to believe it. I was just happy to know === continues to be sane in that comparison.

    • Blackmist@feddit.uk
      link
      fedilink
      English
      arrow-up
      2
      ·
      11 months ago

      You have to remember that the underlying principle of JavaScript seemed to be “never throw an error”, even if what it’s being told to do is weapons grade bollocks.

  • Bye@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    11 months ago

    I don’t work with web stuff, why is js so weird? Can you write a website in other languages, like c# or python?

    • fiah@discuss.tchncs.de
      link
      fedilink
      arrow-up
      11
      ·
      11 months ago

      Can you write a website in other languages, like c# or python?

      sure, as long as it compiles to javascript

    • nintendiator@feddit.cl
      link
      fedilink
      English
      arrow-up
      1
      ·
      11 months ago

      Can you write a website in other languages, like c# or python?

      Yeah, anything that outputs HTML and CSS can do so. There’s a module for Apache to write webpages in Python (libapache-mod-python) and I’m p sure someone somewhere made a module to do it in Rust already except they’re infighting over whether tag parsing in it should be marked unsafe.

      For that matter you do can write web pages in your shell eg.: bash, that’s what CGI is all about.

    • wabafee@lemm.ee
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      11 months ago

      I guess why it’s weird because of the loose rules it follows, like what is mentioned about === and ==. There is WebAssembly which kinda acts like Javabyte code or CIL there used to be huge hype that it’s going to replace JavaScript, though it’s not used that much today. I think why there is low adoption is mainly because JavaScript is good enough, it’s widespread and easy to learn.

    • thanks_shakey_snake@lemmy.ca
      link
      fedilink
      arrow-up
      1
      ·
      11 months ago

      Most of the weirdness comes from being designed for the web, and specifically for working with forms. The value of a form field will always be a string, which is a simple and straightforward idea, but then the trouble showed up when we tried to make it more convenient to work that way.

      • cmdrkeen@programming.devOP
        link
        fedilink
        arrow-up
        2
        ·
        11 months ago

        Actually, most of the weirdness comes from having been originally designed in a matter of 10 days by a single engineer working to accommodate a tight release schedule.

        • thanks_shakey_snake@lemmy.ca
          link
          fedilink
          arrow-up
          0
          ·
          11 months ago

          I mean, do you think that has more explanatory power though? The type coercion rules are actually more elaborate with == than necessary for equality checking, because it was intended as a clever convenience for working with strings. If it was really all about the short timeline, wouldn’t you just skip that and do a more straightforward equality comparison, like the algorithm that === implements?

          Besides, it’s not like everything in the language was conceived and implemented in those 10 days. The language has been evolving steadily since then. I’m not even sure if the modern == comparison algorithm worked that way in the first iteration.

          Personally, I find it more useful to understand the context that lets me say “that’s a quirky consequence of a sensible principle,” rather than blaming it on the “ten days” legend generically.

          • BatmanAoD@programming.dev
            link
            fedilink
            arrow-up
            0
            ·
            11 months ago

            I think the “ten days” explanation has the merit of being charitable, because it implies that Brendan Eich wouldn’t have made such short-sighted design choices under more favorable circumstances.

            (I do not believe that it’s a “sensible principle” to treat text as such a fundamental form of data that a basic language feature like the equality operator should be entirely shaped around it. Surely the consequences of building an entire language around text manipulation should be apparent by considering how awkward Posix-style shells are for any nontrivial scripts.)

            • thanks_shakey_snake@lemmy.ca
              link
              fedilink
              arrow-up
              2
              ·
              11 months ago

              Well… The circumstances were that he was asked to whip up a little scripting language, that felt a little like Java and a little like Scheme, which could be used to add simple manipulations and interactions to web pages. Specifically to web pages. Not webservers, mobile apps, databases, banking systems, physics simulations, robotics… Only web pages. And nobody had even conceived yet of something like Google Sheets-- It was simple HTML forms and DOM manipulation.

              IMO in that context, it makes alot of sense. I think it was probably still the wrong decision-- definitely with the benefit of hindsight, and quite possibly even at the time, even in that narrow context. Way more trouble than it’s worth.

              But it’s beneficial to know that there was a principled (if misguided) reason behind it, that ties into the nature and history of the language-- It’s not simply “dude was in a hurry and not thinking.” Both are kinda true, but the former perspective helps us understand something useful, whereas the latter doesn’t get us anywhere interesting.

              • cmdrkeen@programming.devOP
                link
                fedilink
                arrow-up
                3
                ·
                11 months ago

                TBF he probably had know way of knowing that the language he was creating would one day end up being as popular as it is now.

                I guess the moral of the story is that you can never really predict what long term consequences your decisions might have down the road.

  • Sanchokan@kbin.social
    link
    fedilink
    arrow-up
    1
    arrow-down
    1
    ·
    11 months ago

    A few years back I came to the conclusion that the holy trinity in Christianity are three levels of abstraction: the son => God walks on earth and tangible, the father => God in heaven untangible but still reachable by speech, holy spirit => God in who knows where.

    Then I thought that as a way of imparting the thought that any level of abstraction of the universe would also be inhabitated by God, those which we can sense, and those where our senses can’t reach. The idea that omniprescense is not only limited to our dimension.

    I’m not sure if that is the original meaning but is a way of seeing it that I can relate to, since I’ve always been akeen to a more abstract idea of God, and not so much to a figure that praises itself of thought, which is a human attribute.