Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

agnos.is Forums

  1. Home
  2. Programmer Humor
  3. JavaScript

JavaScript

Scheduled Pinned Locked Moved Programmer Humor
programmerhumor
152 Posts 83 Posters 2 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S [email protected]

    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.

    R This user is from outside of this forum
    R This user is from outside of this forum
    [email protected]
    wrote on last edited by [email protected]
    #96

    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.

    S 1 Reply Last reply
    0
    • V [email protected]

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

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

      / jk 😁

      C This user is from outside of this forum
      C This user is from outside of this forum
      [email protected]
      wrote on last edited by
      #97

      Use typescript if you're paranoid about this

      1 Reply Last reply
      1
      • R [email protected]

        I'd rather have my website shit itself than have silent difficult to find errors.

        C This user is from outside of this forum
        C This user is from outside of this forum
        [email protected]
        wrote on last edited by
        #98

        Use typescript

        1 Reply Last reply
        1
        • K [email protected]

          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

          C This user is from outside of this forum
          C This user is from outside of this forum
          [email protected]
          wrote on last edited by
          #99

          Use typescript

          T 1 Reply Last reply
          2
          • J [email protected]

            In practice runtime errors are a bitch to find and fix.

            C This user is from outside of this forum
            C This user is from outside of this forum
            [email protected]
            wrote on last edited by
            #100

            Fair enough. This is why people prefer typescript

            1 Reply Last reply
            1
            • R [email protected]

              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.

              S This user is from outside of this forum
              S This user is from outside of this forum
              [email protected]
              wrote on last edited by
              #101

              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

              Well, technically this is the explanation, it really isn't a good one.

              x + 1 with x not being defined also doesn't result in a NaN but instead it throws a reference error, even though that undefined variable isn't a number either. And x = 1;x.toUpperCase(); also doesn't silently do anything, even though in this case it could totally return "1" by coercing x to a string first. Instead it throws a TypeError.

              It's really only around number handling where JS gets so weird.

              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.

              That's exactly the thing. It's not obvious what the person wants and a NaN is most likely not what the person wants at either. So what's the point in defaulting to something they certainly didn't want instead of making it obvious that the input made no sense?

              A similarly ambiguous situation would be something like x = 2 y. For someone with a mathematical background this clearly looks like x = 2 * y with an implicit multiplication sign. But it's not in the JS standard to interpret implicit multiplication signs. If you want multiplication, it needs to explicitly use the sign. And thus JS dutifully throws a Syntax Error instead of just guessing what the programmer maybe wanted.

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

              My main point here was that if you have mathematical symbols for string operations, all of the acceptable operations using mathematical symbols need to be string operations. Like e.g. "ab" * 2 => "abab", which many languages provide. That's consistent. I didn't mean that all of these operators need to be implemented, but if they aren't they should throw an error (I stated that in my original comment).

              What's an issue here is that "1" + 1 does a string concatenation, while "1" - 1 converts to int and does a math operation. That's inconsistent. Because even you want to use that feature, you will stumble over + not performing a math operation like -.

              So it should either be that +/- always to math operations and you have a separate operator (e.g. . or ..) for concatenation, or if you overload + with string operations, all of the operators that don't throw an exception need to be strictly string-operations-only.

              1 Reply Last reply
              1
              • R [email protected]

                You are entitled to your opinion. implicit conversion to string is not a feature in most languages for good reasons.

                3 This user is from outside of this forum
                3 This user is from outside of this forum
                [email protected]
                wrote on last edited by
                #102

                Sure. And you're entitled to yours. But words have meaning and this isn't MY OPINION, it's objective reality. It follows strict rules for predictable output, it is not nonsensical.

                You're entitled to think it's nonsense, and you'd be wrong. You don't have to like implicit type coercion, but it's popular and in many languages for good reason...

                Language Implicit Coercion Example
                JavaScript '5' - 1 β†’ 4
                PHP '5' + 1 β†’ 6
                Perl '5' + 1 β†’ 6
                Bash $(( '5' + 1 )) β†’ 6
                Lua "5" + 1 β†’ 6
                R "5" + 1 β†’ 6
                MATLAB '5' + 1 β†’ 54 (ASCII math)
                SQL (MySQL) '5' + 1 β†’ 6
                Visual Basic '5' + 1 β†’ 6
                TypeScript '5' - 1 β†’ 4
                Tcl "5" + 1 β†’ 6
                Awk '5' + 1 β†’ 6
                PowerShell '5' + 1 β†’ 6
                ColdFusion '5' + 1 β†’ 6
                VBScript '5' + 1 β†’ 6
                ActionScript '5' - 1 β†’ 4
                Objective-J '5' - 1 β†’ 4
                Excel Formula "5" + 1 β†’ 6
                PostScript (5) 1 add β†’ 6

                I think JavaScript is filthy, I'm at home with C#, but I understand and don't fear ITC.

                R 2 Replies Last reply
                2
                • 3 [email protected]

                  Sure. And you're entitled to yours. But words have meaning and this isn't MY OPINION, it's objective reality. It follows strict rules for predictable output, it is not nonsensical.

                  You're entitled to think it's nonsense, and you'd be wrong. You don't have to like implicit type coercion, but it's popular and in many languages for good reason...

                  Language Implicit Coercion Example
                  JavaScript '5' - 1 β†’ 4
                  PHP '5' + 1 β†’ 6
                  Perl '5' + 1 β†’ 6
                  Bash $(( '5' + 1 )) β†’ 6
                  Lua "5" + 1 β†’ 6
                  R "5" + 1 β†’ 6
                  MATLAB '5' + 1 β†’ 54 (ASCII math)
                  SQL (MySQL) '5' + 1 β†’ 6
                  Visual Basic '5' + 1 β†’ 6
                  TypeScript '5' - 1 β†’ 4
                  Tcl "5" + 1 β†’ 6
                  Awk '5' + 1 β†’ 6
                  PowerShell '5' + 1 β†’ 6
                  ColdFusion '5' + 1 β†’ 6
                  VBScript '5' + 1 β†’ 6
                  ActionScript '5' - 1 β†’ 4
                  Objective-J '5' - 1 β†’ 4
                  Excel Formula "5" + 1 β†’ 6
                  PostScript (5) 1 add β†’ 6

                  I think JavaScript is filthy, I'm at home with C#, but I understand and don't fear ITC.

                  R This user is from outside of this forum
                  R This user is from outside of this forum
                  [email protected]
                  wrote on last edited by
                  #103

                  C# is filthy. But it explains where you got your warped idea of righteousness.

                  1 Reply Last reply
                  0
                  • 3 [email protected]

                    Sure. And you're entitled to yours. But words have meaning and this isn't MY OPINION, it's objective reality. It follows strict rules for predictable output, it is not nonsensical.

                    You're entitled to think it's nonsense, and you'd be wrong. You don't have to like implicit type coercion, but it's popular and in many languages for good reason...

                    Language Implicit Coercion Example
                    JavaScript '5' - 1 β†’ 4
                    PHP '5' + 1 β†’ 6
                    Perl '5' + 1 β†’ 6
                    Bash $(( '5' + 1 )) β†’ 6
                    Lua "5" + 1 β†’ 6
                    R "5" + 1 β†’ 6
                    MATLAB '5' + 1 β†’ 54 (ASCII math)
                    SQL (MySQL) '5' + 1 β†’ 6
                    Visual Basic '5' + 1 β†’ 6
                    TypeScript '5' - 1 β†’ 4
                    Tcl "5" + 1 β†’ 6
                    Awk '5' + 1 β†’ 6
                    PowerShell '5' + 1 β†’ 6
                    ColdFusion '5' + 1 β†’ 6
                    VBScript '5' + 1 β†’ 6
                    ActionScript '5' - 1 β†’ 4
                    Objective-J '5' - 1 β†’ 4
                    Excel Formula "5" + 1 β†’ 6
                    PostScript (5) 1 add β†’ 6

                    I think JavaScript is filthy, I'm at home with C#, but I understand and don't fear ITC.

                    R This user is from outside of this forum
                    R This user is from outside of this forum
                    [email protected]
                    wrote on last edited by
                    #104

                    Also, you contradicted yourself just then and there. Not a single of your examples does string concatenation for these types. It's only JS

                    B 1 Reply Last reply
                    1
                    • C [email protected]

                      Use typescript

                      T This user is from outside of this forum
                      T This user is from outside of this forum
                      [email protected]
                      wrote on last edited by [email protected]
                      #105

                      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.

                      1 Reply Last reply
                      7
                      • cm0002@lemmy.worldC [email protected]
                        This post did not contain any content.
                        R This user is from outside of this forum
                        R This user is from outside of this forum
                        [email protected]
                        wrote on last edited by
                        #106

                        Imagine doing math with strings and then blaming the language not yourself

                        Z gmtom@lemmy.worldG 2 Replies Last reply
                        35
                        • R [email protected]

                          Imagine doing math with strings and then blaming the language not yourself

                          Z This user is from outside of this forum
                          Z This user is from outside of this forum
                          [email protected]
                          wrote on last edited by
                          #107

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

                          F 1 Reply Last reply
                          42
                          • Z [email protected]

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

                            F This user is from outside of this forum
                            F This user is from outside of this forum
                            [email protected]
                            wrote on last edited by
                            #108

                            Sure, but at this point it's your own fault if you don't use Typescript to keep these issues from happening.

                            S J 2 Replies Last reply
                            11
                            • F [email protected]

                              Sure, but at this point it's your own fault if you don't use Typescript to keep these issues from happening.

                              S This user is from outside of this forum
                              S This user is from outside of this forum
                              [email protected]
                              wrote on last edited by
                              #109

                              So, just don’t use JavaScript?

                              F M S 3 Replies Last reply
                              8
                              • F [email protected]

                                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.

                                G This user is from outside of this forum
                                G This user is from outside of this forum
                                [email protected]
                                wrote on last edited by
                                #110

                                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.

                                1 Reply Last reply
                                0
                                • S [email protected]

                                  So, just don’t use JavaScript?

                                  F This user is from outside of this forum
                                  F This user is from outside of this forum
                                  [email protected]
                                  wrote on last edited by
                                  #111

                                  I wouldn't use raw JS for anything new, yes. Typescript however is an excellent language.

                                  1 Reply Last reply
                                  3
                                  • L [email protected]

                                    I don't even know Haskell but it seems like (" ( , ) ") would be an instance of boob.

                                    B This user is from outside of this forum
                                    B This user is from outside of this forum
                                    [email protected]
                                    wrote on last edited by
                                    #112

                                    (.) is a valid expression in Haskell. Normally it is the prefix form of the infix operator . that does function
                                    composition. (.) (2*) (1+) 3 = ((2*) . (1+)) 3 = 2 * (1 + 3) = 8.

                                    But, the most common use of the word "boob" in my experience in Haskell is the "boobs operator": (.)(.). It's usage in Haskell is limited (tho valid), but it's appearance in racy ASCII art predates even the first versions on Haskell.

                                    L 1 Reply Last reply
                                    0
                                    • K [email protected]

                                      That is just the tip of the iceberg:

                                      https://whatthefsharp.com/JavaScript/3

                                      W This user is from outside of this forum
                                      W This user is from outside of this forum
                                      [email protected]
                                      wrote on last edited by
                                      #113

                                      Haha that’s a great site. But I think the C example is actually reasonable behaviour.

                                      1 Reply Last reply
                                      2
                                      • J [email protected]

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

                                        B This user is from outside of this forum
                                        B This user is from outside of this forum
                                        [email protected]
                                        wrote on last edited by
                                        #114

                                        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.

                                        1 Reply Last reply
                                        1
                                        • R [email protected]

                                          Also, you contradicted yourself just then and there. Not a single of your examples does string concatenation for these types. It's only JS

                                          B This user is from outside of this forum
                                          B This user is from outside of this forum
                                          [email protected]
                                          wrote on last edited by
                                          #115
                                          • In https://lemm.ee/comment/20947041 they claimed "implicit type coercion" and showed many examples; they did NOT claim "string concatenation".
                                          • However, that was in reply to https://lemmy.world/comment/17473361 which was talking about "implicit conversion to string" which is a specific type of "implicit type coercion"; NONE of the examples given involved a conversion to string.
                                          • But also, that was in reply to https://lemm.ee/comment/20939144 which only mentions "implicit type coercion" in general.

                                          So, I think probably everyone in the thread is "correct", but you are actually talking past one another.

                                          I think the JS behavior is a bad design choice, but it is well documented and consistent across implementations.

                                          R 1 Reply Last reply
                                          2
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups