• PieMePlenty@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      1 day ago

      It does to some degree.

      • “11” is string, 1 is an int, because strings can be added (+) convert int to string and combine: “11”+“1” = “111”
      • “11” is string, 1 is an int, because strings cant be subtracted (-) convert string to int and combine: 11-1 = 10

      I’m not into JS so I don’t know how it takes priority. ints can be added too, so I guess its basing it on the first variable which is compatible with the operator: in the first case string, in the second case int.

      If this is how it works, it makes sense. But imo its a case of the designers being preoccupied with whether or not they could, they didn’t stop to think if they should.

    • zagaberoo@sopuli.xyz
      link
      fedilink
      arrow-up
      0
      ·
      2 days ago

      The risk is when it happens unintentionally. The language is bad for hiding such errors by being overly ‘helpful’ in assuming intent.

          • FooBarrington@lemmy.world
            link
            fedilink
            arrow-up
            0
            ·
            2 days ago

            Not really, considering Typescript only adds static types to JS. It’s not a different language, it’s an extension.

            • Quibblekrust@thelemmy.club
              link
              fedilink
              English
              arrow-up
              1
              ·
              2 days ago

              Since it needs to be compiled to JavaScript in order to be used, I kind of consider it a different language. Yes, it’s a strict superset of JavaScript, but that makes it different.

          • matlag@sh.itjust.works
            link
            fedilink
            English
            arrow-up
            1
            ·
            2 days ago

            That’s also my understanding: “Javascript is great because you can use other languages and then transpile them to JS.”

    • Alexstarfire@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      3 days 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.

    • Gsus4@mander.xyz
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      3 days 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 🤭

      • dalekcaan@lemm.ee
        link
        fedilink
        arrow-up
        1
        ·
        3 days ago

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

        • Gsus4@mander.xyz
          link
          fedilink
          arrow-up
          2
          ·
          edit-2
          3 days 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 😇

          • palordrolap@fedia.io
            link
            fedilink
            arrow-up
            0
            ·
            3 days ago

            it commutes

            Maybe the behaviour with regard to type conversion, but not for the operation itself.

            “13”+12 and 12+“13” don’t yield the same result.

  • whaleross@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    3 days ago

    People that try to do mathematical operations with strings blaming the programming language that had a stated design goal to do its best and try to keep running scripts that make no sense because they realized it would be used by people that have no idea what they are doing. Clearly they were right.

    • 0x0@lemmy.zip
      link
      fedilink
      arrow-up
      0
      ·
      3 days ago

      it would be used by people that have no idea what they are doing. Clearly

      And so let’s enable these people?
      Let’s add AI to the mix while we’re at it.

      • whaleross@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        3 days ago

        Now that you mention it, it is a bit funny how Lemmy is hating LLMs as a code generation tool while also hating on the interpreter for their own hand typed code not running.

          • whaleross@lemmy.world
            link
            fedilink
            arrow-up
            1
            ·
            edit-2
            3 days ago

            Then you do not do Javascript, because it is an interpreted language.

            Edit: or Python, or a command line shell, or any CORS, or databases, or… Well idk really what you do use honestly.

    • grue@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      3 days ago

      the programming language that had a stated design goal to do its best and try to keep running scripts that make no sense…

      …itself makes no sense. It is wrong and bad that Javascript was ever designed that way in the first place.

      • whaleross@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        3 days ago

        It was never intended to run full applications but only the small business scripts and hobbyist homepage stuff that were the thing in the 90s, across inconsistent browsers that were a jungle of hit and miss behaviour where it was preferred that menus keep working even if the mouse effect was not. Anything of scale was expected to be done in Java. Dynamic web pages did not exist and as anything not static was generated server side into a static html file to be rendered on the client.

        Anyway, back then it wasn’t considered the job of the programming language to hold the hand of the aspiring developer as it is common today. It’s not a bad thing that IDE and even compilers and preprocessors try to help you write better code today, but then it simply didn’t exist.

        JavaScript is from a different time and because it has the hard requirement or backwards compatibility there is no changing it and has not been for thirty years except to add stuff to it.

        I think it’s just silly to ask the past to keep up with the present. Bad code is not the fault of the language regardless, even though junior devs and even seasoned ones like to think so to protect their ego. I think it is better to accept it, learn from it and roll with it because every single platform and language has their weird quirks anyway.

        Signed, old dude that learned programming in 8 bit BASIC and 6502 machine code without an assembler, where code bad enough would freeze your machine that required a cold boot and starting over from your last save that you didn’t do.

      • squaresinger@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        3 days ago

        Especially that + and - act differently. If + does string concattenation, - should also do some string action or throw an error in this situation.

        • rdri@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          3 days ago
          • should also do some string action

          Like what kind of string action?

          “Hello” + " world" is what everyone can understand. Switch with “-” and it becomes pointless.

            • rdri@lemmy.world
              link
              fedilink
              arrow-up
              0
              ·
              3 days ago

              If you try what I wrote it will throw a NaN. I was asking about the first part of the proposal.

              • squaresinger@lemmy.world
                link
                fedilink
                arrow-up
                1
                ·
                2 days ago

                The NaN isn’t an thrown. It’s just silently put into the result. And in this case it’s completely unintelligible. Why would an operation between two strings result in a number?

                "Hello" - "world" is an obvious programmer mistake. The interpreter knows that this is not something anyone will ever do on purpose, so it should not silently handle it.

                The main problem here is downward coercion. Coercion should only go towards the more permissive type, never towards the more restrictive type.

                Coercing a number to a string makes sense, because each number has a representation as a string, so "hello" + 1 makes intuitive sense.

                Coercing a string to a number makes no sense, because not every string has a representation as a number (in fact, most strings don’t). "hello" - 1 makes no sense at all. So converting a string to a number should be done by an explicit cast or a conversion function. Using - with a string should always result in a thrown error/exception.

                • rdri@lemmy.world
                  link
                  fedilink
                  arrow-up
                  0
                  ·
                  edit-2
                  2 days ago

                  The interpreter knows that this is not something anyone will ever do on purpose, so it should not silently handle it.

                  You basically defied the whole NaN thing. I may even agree that it should always throw an error instead, but… Found a good explanation by someone:

                  NaN is the number which results from math operations which make no sense

                  And the above example fits that.

                  "hello" - 1 makes no sense at all.

                  Yeah but actually there can be many interpretations of what someone would mean by that. Increase the bytecode of the last symbol, or search for “1” and wipe it from string. The important thing is that it’s not obvious what a person who wrote that wants really, without additional input.

                  Anyway, your original suggestion was about discrepancy between + and - functionality. I only pointed out that it’s natural when dealing with various data types.

                  Maybe it is one of the reasons why some languages use . instead of + for strings.