• atx_aquarian@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    10 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
      3
      ·
      10 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.

      • masterspace@lemmy.ca
        link
        fedilink
        English
        arrow-up
        1
        ·
        edit-2
        10 months ago

        The explanation given to you makes it sound like == was deliberately designed to be a more convenient version of ===

        I mean technically == was deliberately designed to be a more convenient version of other languages’ == operator… Just specifically more convenient for light UI stuff since that was all JavaScript was supposed to be used for at the time (or all they thought it would be used for).

        But give programmers a way to write and execute a small script and someone will eventually use that to try and write an emulator that emulates the computer it’s running on, so the web evolved into more complicated applications, and then that convenience turned out to be wildly inconvenient, not to mention horribly unexpected for programmers coming from other languages, so then they added the triple equality to match other languages.