• katy ✨
    link
    fedilink
    2
    edit-2
    11 hours ago

    javascript is to web developers what powerpoint is to sales people

  • @arc@lemm.ee
    link
    fedilink
    14
    edit-2
    1 day ago

    Javascript is a dogshit language that everyone is stuck with. The best that we can hope for is the likes of typescript take the edge off of it. Even though it’s like smearing marzipan over a turd. At least it’s ok if you don’t take a deep bite.

    • @zagaberoo@sopuli.xyz
      link
      fedilink
      431 day ago

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

            • @Quibblekrust@thelemmy.club
              link
              fedilink
              English
              51 day 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.

              • @FooBarrington@lemmy.world
                link
                fedilink
                2
                edit-2
                1 day ago

                That’s your prerogative, but it honestly doesn’t make sense. Typescript adds almost no functionality to JS (and the few pieces it adds are now considered mistakes that shouldn’t be used anymore). It only focuses on adding typing information, and in the future you’ll be able to run TS that doesn’t use those few added features as JS (see the proposal).

                You can also add the TS types as comments in your JS code, which IMO shows that it’s not a different language.

          • matlag
            link
            fedilink
            English
            71 day ago

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

            • @FooBarrington@lemmy.world
              link
              fedilink
              1
              edit-2
              1 day ago

              JS itself is great, I prefer it to most other languages due to the flexibility that it allows. Adding types through TS to safeguard against footguns doesn’t mean you’re not still using JS. You can also add the types using comments instead if you prefer it, which means you’re actually writing raw JS.

          • @sebsch@discuss.tchncs.de
            link
            fedilink
            11 day ago

            Yeah! Wasm is a thing. At least rust and go are pretty neat in the browser lately.

            We should leave that pile of semantics and just go further with web development

    • @Gsus4@mander.xyz
      link
      fedilink
      112
      edit-2
      2 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 🤭

      • Avid Amoeba
        link
        fedilink
        402 days ago

        Yup. It’s completely inconsistent in its interpretation of the + operator.

        • @Gsus4@mander.xyz
          link
          fedilink
          19
          edit-2
          2 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
            link
            fedilink
            192 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.

              • palordrolap
                link
                fedilink
                42 days ago

                Given it’s JavaScript, which was expressly designed to carry on regardless, I could see an argument for it returning NaN, (or silently doing what Perl does, like I mention in a different comment) but then there’d have to be an entirely different way of concatenating strings.

                • @ChickenLadyLovesLife@lemmy.world
                  link
                  fedilink
                  English
                  62 days ago

                  expressly designed to carry on regardless

                  I’m surprised they didn’t borrow On Error Resume Next from Visual Basic. Which was wrongly considered to be the worst thing in Visual Basic - when the real worst thing was On Error Resume. On Error Resume Next at least moved on to the next line of code when an error occurred; On Error Resume just executed the error-generating line again … and again … and again … and again …

      • @dalekcaan@lemm.ee
        link
        fedilink
        122 days ago

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

      • @0x0@lemmy.zip
        link
        fedilink
        122 days ago

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

        Kinda like log4j!

    • @Alexstarfire@lemmy.world
      link
      fedilink
      212 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.

      • @grue@lemmy.world
        link
        fedilink
        27
        edit-2
        2 days 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
          82 days 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]
          link
          fedilink
          22 days 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
            21 day 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.

      • Tb0n3
        link
        fedilink
        English
        11
        edit-2
        2 days ago

        It’s an issue with most if not all languages that aren’t strongly typed.

        • palordrolap
          link
          fedilink
          52 days ago

          Perl is an old but notable exception. + is purely for addition in the base language.

          If you try to add two strings with it, they’ll be converted to numbers based on any number-like characters they have at their left hand ends, and, if warnings are enabled (and you should definitely do that), you’ll get runtime warnings about it if there’s even anything vaguely non-numeric about them.

          e.g. “1”+“11” will get you 12 with no complaint, warnings or otherwise. Not even the string “12” either, although it’s hard to determine one from the other in Perl. It’s a need-to-know kind of thing. And you generally don’t.

          “a”+“bb” gives 0 as the result because they’re not numbers and “1a”+“11bb” will give 12, but these latter two will give warnings. Two each, in fact, one for each dodgy parameter.

          String concatenation is done with the dot operator instead. “1”.“11” gives “111”. This comes with it’s own minor problems, but at least + is safe.

          • @nyan@lemmy.cafe
            link
            fedilink
            English
            31 day ago

            That’s because Perl doesn’t do operator overloading in general. Even the equality operator is different for strings (eq instead of ==). As a language, it may look pretty weird and lack some modern features, but the underlying design is surprisingly intelligent and consistent in many ways.

            • palordrolap
              link
              fedilink
              11 day ago

              Not strictly true.

              Perl’s default bitwise operators do differentiate between numbers and strings that look like numbers in a way that addition doesn’t*, and the readline/glob operator <> does different things depending on what (if anything) is between the signs.

              There’s also the whole overload pragma for objects, which doesn’t affect default data types, but if you’re sufficiently perverse, you can define a String class that uses ‘+’ like JavaScript.

              * in 2015, they added new operators so that those and the original operators don’t overload and have only one specific purpose if the bitwise pragma Edit: feature is turned on. You might know all this already though.

    • Cid Vicious
      link
      fedilink
      English
      122 days ago

      I think I’m on the side of “if you do this in your code, you deserve what you get.”

  • kamen
    link
    fedilink
    41 day ago

    If you’re consciously and intentionally using JavaScript like that, I don’t want to be friends with you.

  • @capybara@lemm.ee
    link
    fedilink
    English
    312 days ago

    To start off… Using arithmetic operators on strings in combination with integers is a pure skill issue. Let’s disregard this.

    If you were to use + where one part is a string, it’s natural to assume a string appending is desired since + is commonly used as a function for this. On the other hand, - is never used for any string operation. Therefore, it’s safe to assume that it relates to actual artihmetics and any strings should therefore be converted to numerical values.

    This is an issue with untyped languages. If you don’t like it, use typescript. End of story.

    • @Jankatarch@lemmy.world
      link
      fedilink
      322 days ago

      Instead of trying to make it work, javascript could just say “error.” Being untyped doesn’t mean you can’t have error messages.

      • @bss03@infosec.pub
        link
        fedilink
        English
        21 day ago

        I think it’s less about type system, and more about lack of a separate compilation step.

        With a compilation step, you can have error messages that developers see, but users don’t. (Hopefully, these errors enable the developers to reduce the errors that users see, and just generally improve the UX, but that’s NOT guaranteed.)

        Without a compilation step, you have to assign some semantics to whatever random source string your interpreter gets. And, while you can certainly make that an error, that would rarely be helpful for the user. JS instead made the choice to, as much as possible, avoid error semantics in favor of silent coercions, conversions, and conflations in order to make every attempt to not “error-out” on the user.

        It would be a very painful decade indeed to now change the semantics for some JS source text.

        Purescript is a great option. Typescript is okay. You could also introduce a JS-to-JS “compilation” step that DID reject (or at least warn the developer) for source text that “should” be given an error semantic, but I don’t know an “off-the-shelf” approach for that – other than JSLint.

      • @capybara@lemm.ee
        link
        fedilink
        English
        62 days ago

        This is fair enough from an idealistic view. In practice, you don’t want your entire website to shit itself because of a potentially insignificant error.

        • @Kacarott@aussie.zone
          link
          fedilink
          222 days ago

          This is exactly why it should throw an error, to make it incredibly obvious something isn’t working correctly so it can be fixed. Otherwise you have wrong logic leading to hard to notice and hard to debug problems in your code

            • @TheBeege@lemmy.world
              link
              fedilink
              8
              edit-2
              1 day ago

              No. I don’t want to transpile. I don’t want a bundle. I want a simple site that works in the browser. I want to serve it as a static site. I don’t want a build step. I don’t want node_modules. I want to code using the language targeted for the platform without any other nonsense.

              Javascript is cancer. Fucking left pad?! How the fuck did we let that happen? What is this insane fucking compulsion to have libraries for two lines of code? To need configuration after configuration just to run fucking hello world with types and linting?

              No, fuck Typescript. Microsoft owns enough. They own where you store your code. They own your IDE. They might own your operating system. Too much in one place. They don’t need to own the language I use, too.

              “Let’s use a proprietary improvement to fix the standard that should have not sucked in the first place” is why we can’t have nice things.

              No.

        • @Valmond@lemmy.world
          link
          fedilink
          52 days ago

          Look! I bought this for free on capybaras website, there’s a glitch!

          capybara: at least it didn’t throw an error.

          / jk 😁

  • @bitjunkie@lemmy.world
    link
    fedilink
    2
    edit-2
    1 day ago

    It’s because + is two different operators and overloads based on the type to the left, while - is only a numeric operator and coerces left and right operands to numeric. But frankly if you’re still using + for math or string concatenation in 2025, you’re doing it wrong.

    • @Hadriscus@lemm.ee
      link
      fedilink
      81 day ago

      I know nothing about javascript, what is wrong with using + for math? perhaps naively, I’d say it looks suited for the job

        • @bitjunkie@lemmy.world
          link
          fedilink
          0
          edit-2
          1 day ago

          Point taken but the one I use is only ~200k for the whole package, ~11k for the actual file that gets loaded

      • @Quibblekrust@thelemmy.club
        link
        fedilink
        English
        61 day ago

        It’s much better to make your own function that uses bitwise operations to do addition.

        function add(a, b) {
            while (b !== 0) {
                // Calculate carry
                let carry = a & b;
        
                // Sum without carry
                a = a ^ b;
        
                // Shift carry to the left
                b = carry << 1;
            }
            return a;
        }
        

        (For certain definitions of better.)

  • @whaleross@lemmy.world
    link
    fedilink
    432 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.

    • @grue@lemmy.world
      link
      fedilink
      372 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
        222 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.

        • @BorgDrone@lemmy.one
          link
          fedilink
          132 days ago

          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.

          But that’s exactly what it’s doing by trying to figure out what the developer meant. ‘“11” + 1’, should cause the compiler to tell the developer to to fuck themselves.

        • Executing after undefined behavior is arguably worse than terminating with an exception. A terminated script can’t leak data or wreak havoc in other ways.

    • @0x0@lemmy.zip
      link
      fedilink
      112 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
        62 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
            4
            edit-2
            2 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.

            • @0x0@lemmy.zip
              link
              fedilink
              62 days ago

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

              No shit?! Wow… who would’ve known…

  • @proctor1432@lemmy.world
    link
    fedilink
    122 days ago

    Heck, I need to learn some new languages apparently. Here I was expecting an angry "CS0029 cannot implicitly convert type ‘string’ to ‘int’!