• Gsus4@mander.xyz
      link
      fedilink
      arrow-up
      98
      ·
      8 months ago

      Hm, playing devil’s advocate, I think it is because the minus has not been defined as a string operation (e.g. it could pop the last char), so it defaults to the mathematical operation and converts both inputs into ints.

      The first is assumed to be a concat because one of the parcels is a string…

      It’s just doing a lot of stuff for you that it shouldn’t be in first place 🤭

        • Gsus4@mander.xyz
          link
          fedilink
          arrow-up
          18
          ·
          edit-2
          8 months ago

          Yeah, I actually had to try 1+“11” to check that it didn’t give me 12, but thankfully it commutes it’s consistent 😇

      • dalekcaan@lemm.ee
        link
        fedilink
        arrow-up
        13
        arrow-down
        1
        ·
        8 months ago

        Yeah, this looks dumb on the surface, but you’ve got bigger problems if you’re trying to do math with strings

    • Alexstarfire@lemmy.world
      link
      fedilink
      arrow-up
      20
      arrow-down
      2
      ·
      8 months ago

      Unfortunately, it makes sense if you know what + means, which is concatenate. - is strictly a math function though.

      Not saying that makes this better. It just makes sense.

      • grue@lemmy.world
        link
        fedilink
        arrow-up
        26
        arrow-down
        3
        ·
        8 months ago

        It is ‘comprehensible’ in the sense that it’s possible to figure out how it happened, but it absolutely does not “make sense” in terms of being a reasonable language design decision. It’s 100% incompetence on the part of the person who created Javascript.

        • Alexstarfire@lemmy.world
          link
          fedilink
          arrow-up
          6
          ·
          8 months ago

          I mean, I’d never try to do this anyway because if the types aren’t the same unexpected things can happen. That’s like programming 101.

        • Fushuan [he/him]@lemm.ee
          link
          fedilink
          arrow-up
          2
          ·
          8 months ago

          It makes perfect sense if the Lang objective is to fail as little as possible. It picks the left side object, checks if the operand is a valid operand of the type. If it is, it casts the right variable into that type and perform the operand. If it isn’t, it reverses operand positions and tries again.

          The issue here is more the fact that + is used both as addition and as concatenation with different data types. Well, not an issue, just some people will complain.

          • grue@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            8 months ago

            Computing a nonsensical result is itself a failure. Continuing to run while avoiding giving an error in that case accomplishes nothing but to make the program harder to debug.