• blackn1ght@feddit.uk
        link
        fedilink
        arrow-up
        10
        ·
        11 months ago

        Because in reality you’re not doing stupid stuff like that in the image. And using Typescript definitely helps.

        However I’m always annoyed that the month parameter when constructing a date object is 0 based. So 1st of Jan is

        new Date(2024, 0, 1)
        
        • BaardFigur@lemmy.world
          link
          fedilink
          arrow-up
          8
          ·
          edit-2
          11 months ago

          However I’m always annoyed that the month parameter when constructing a date object is 0 based. So 1st of Jan is

          new Date(2024, 0, 1)
          

          That’s nothing. The ‘tm’ type in the C standard library also starts months at 0. But the year 0 is the actually the year 1900.

          So 1. january 2024 would be

          tm date{ .tm_mday=1, .tm_mon=0, .tm_year=124 };.

        • JaddedFauceet@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          11 months ago

          Looks confusing at first, but I found it nice for accessing a month array.

          const months = ["Jan", "Feb", ...];
          
          months[0] === "Jan";
          
          const label = months[date.getMonth()];
          
      • darcy@sh.itjust.works
        link
        fedilink
        arrow-up
        7
        ·
        11 months ago

        almost forced to for web front end. why you would use it anywhere else, however, i will never know

        • Turun@feddit.de
          link
          fedilink
          arrow-up
          4
          ·
          11 months ago

          The same reason people drive their car to buy groceries.

          You bought it for something where it was the only option, driving 30km to work everyday. But ever since you got it, the trip to the super market is kinda too hot in the summer and too cold in the winter and what if you spontaneously need to buy more than expected?

          People learn it for front end dev, and then they use what they know for back end too.

  • Lunya \ she/it@iusearchlinux.fyi
    link
    fedilink
    arrow-up
    46
    ·
    edit-2
    11 months ago

    I still don’t understand the === operator

    Edit: I think a more type strict ==? Pretty sure I understand the point of typescript now.

    • SzethFriendOfNimi@lemmy.world
      link
      fedilink
      arrow-up
      127
      ·
      edit-2
      11 months ago

      So in JavaScript there’s the assignment

      =
      

      and the comparator is

      ==
      

      Since there’s no types JS will do implicit conversion before comparison when using == in a case like this

      if(false == '0'){
          //this is true
      }
      

      But with === it doesn’t. It means literally compare these

      if(false === '0'){
          //this is false
      }else{
          //so this will execute instead 
      }
      

      But this, however, will

      var someState = false;
       if(someState === false){
          //this is true
      }
      
    • QuazarOmega@lemy.lol
      link
      fedilink
      arrow-up
      62
      ·
      11 months ago
      > 1 == 1
      true
      > 1 == '1'
      true
      > 1 === '1'
      false
      

      (from node REPL)

      Basically it’s the real equals sign perfection

    • frezik@midwest.social
      link
      fedilink
      arrow-up
      45
      ·
      11 months ago

      The short answer is that your language needs === when it fucked up the semantics of ==, but it’s also too popular and you can’t fix it without breaking half the web.

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

        Or when it is something like Prolog, where equality is inherently a messy and complex concept.

    • kevincox@lemmy.ml
      link
      fedilink
      arrow-up
      20
      ·
      11 months ago

      JS’s == has some gotchas and you almost never want to use it. So === is what == should have been.

      All examples are true:

      "1" == true
      [1, 2] == "1,2" 
      " " == false
      null == undefined 
      

      It isn’t that insane. But some invariants that you may expect don’t hold.

      "" == 0
      "0" == 0
      "" != "0" 
      
      • Feathercrown@lemmy.world
        link
        fedilink
        English
        arrow-up
        5
        ·
        11 months ago

        One neat feature is you can compare to both null and undefined at the same time, without other falsey values giving false positives. Although that’s not necessary as often now that we have nullish coalescing and optional chaining.

        • kevincox@lemmy.ml
          link
          fedilink
          arrow-up
          2
          ·
          11 months ago

          I just tested and Terser will convert v === null || v === undefined to null==v. Personally I would prefer to read the code that explicitly shows that it is checking for both and let my minifier/optimizer worry about generating compact code.

    • Mikina@programming.dev
      link
      fedilink
      arrow-up
      18
      ·
      edit-2
      11 months ago

      It’s also important if you’re checking hashes (at least, it was - if you’re using correct hashing algorithm that isn’t ancient, you will not have this problem).

      Because if you take for example “0e462097431906509019562988736854” (which is md5(“240610708”), but also applicable to most other hashing algorithms that hash to a hex string), if(“0e462097431906509019562988736854” == 0) is true. So any other data that hashes to any variantion of “0e[1-9]+” will pass the check, for example:

      md5("240610708") == md5("hashcatqlffzszeRcrt")

      that equals to

      "0e462097431906509019562988736854" == "0e242700999142460696437005736231"

      which thanks to scientific notation and no strict type checking can also mean

      0462097431906509019562988736854 == 0242700999142460696437005736231

      which is

      0 == 0 `

      I did use md5 as an example because the strings are pretty short, but it’s applicable to a whole lot of other hashes. And the problem is that if you use one of the strings that hash to a magic hash in a vulnerable site, it will pass the password check for any user who’s password also hashes to a magic hash. There’s not really a high chance of that happening, but there’s still a lot of hashes that do hash to it.

    • BougieBirdie
      link
      fedilink
      English
      arrow-up
      6
      ·
      11 months ago

      The other comments explains it in pretty good detail, but when I was learning my teacher explained it sort of like a mnemonic.

      1 + 1 = 2 is read “one plus one equals two”

      1 + 1 == 2 is read “one plus one is equal to two”

      1 + 1 === 2 is read “one plus one is really equal to two”

      And you hit the nail on the head, is that === is type explicit while == is implicit.

      • bobbykjack@programming.dev
        link
        fedilink
        arrow-up
        3
        ·
        edit-2
        11 months ago

        I’d use something like:

        = becomes

        == equals

        === is identical to

        It’s funny how everyone thinks “equals” in this context should be “identical to” when, in normal language, it doesn’t really mean that at all!

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

    1 + false ? (I have no idea in which order JS would evaluate things as I rarely have to touch that language much anymore)