• Victor
      link
      fedilink
      326 days ago

      Classic people who don’t know how to code wat. Passing a number in place of a string argument because they don’t know what they’re doing.

      • @qqq@lemmy.world
        link
        fedilink
        11
        edit-2
        26 days ago

        Could be a variable from somewhere else in the code. It should throw type error of some sort if it’s not going to handle a float correctly

        • Victor
          link
          fedilink
          126 days ago

          Agreed, functions in general should do this, and some do. But it should probably be automatic. And the variable argument is a good one, a very good argument for TypeScript. ❤️

      • @jjjalljs@ttrpg.network
        link
        fedilink
        2926 days ago

        Javascript could throw an error to alert you that the input is supposed to be a string, like most languages would do.

        • @heavy@sh.itjust.works
          link
          fedilink
          English
          -326 days ago

          Theoretically, Javascript is an untyped language, so there aren’t supposed to really be static types. Giving type errors in this situation would be against design.

        • Victor
          link
          fedilink
          -2
          edit-2
          26 days ago

          But you’re calling a function specifically made for passing a string to an int… 😆 There’s gotta be some common sense somewhere here, guys.

          Still, it’s a very good point. JS should do this.

          I would suspect one reason it doesn’t do this is to be backwards compatible.

    • @barsoap@lemm.ee
      link
      fedilink
      8
      edit-2
      26 days ago

      https://en.wikipedia.org/wiki/Principle_of_least_astonishment

      …and of course JS made it into the examples, how could it not:

      A programming language’s standard library usually provides a function similar to the pseudocode ParseInteger(string, radix), which creates a machine-readable integer from a string of human-readable digits. The radix conventionally defaults to 10, meaning the string is interpreted as decimal (base 10). This function usually supports other bases, like binary (base 2) and octal (base 8), but only when they are specified explicitly. In a departure from this convention, JavaScript originally defaulted to base 8 for strings beginning with “0”, causing developer confusion and software bugs. This was discouraged in ECMAScript 3 and dropped in ECMAScript 5.

    • @jsomae@lemmy.ml
      link
      fedilink
      5
      edit-2
      26 days ago

      Okay but this documentation is obviously wrong from the first sentence

      The parseInt() function parses a string argument and returns an integer of the specified radix

      Integers don’t have radices. It should read:

      The parseInt() function parses a string argument representing an integer of the specified radix and returns that integer.

      Either way, I still don’t understand the behaviour in the image. nvm, thanks m_f@discuss.online

    • @tauonite@lemmy.world
      link
      fedilink
      7
      edit-2
      26 days ago

      Holy fuck that is long. When the documentation for the integer parsing function is 10 pages long, there’s something seriously wrong with the language

      • Lemminary
        link
        fedilink
        326 days ago

        Is it? I’ve seen longer articles for C# and not as many complaints about it.

        • @barsoap@lemm.ee
          link
          fedilink
          226 days ago

          Probably not an article about integer parsing, though. If the docs are that long, then because Microsoft does have a tendency to be overly verbose for things they think you need, just to have no docs for the stuff you actually need.

          For reference here’s the relevant rust docs.

    • @FiskFisk33@startrek.website
      link
      fedilink
      326 days ago

      oh god the reason is even stupider then I expected

      Because large numbers use the e character in their string representation (e.g., 6.022e23 for 6.022 × 1023), using parseInt to truncate numbers will produce unexpected results when used on very large or very small numbers. parseInt should not be used as a substitute for Math.trunc().