• Jankatarch@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      10 hours ago

      I actually don’t hate this. Like I love C because it lets me mess around with how stuff internally work. This just sounds like a fun concept. If it was 5000000000000 would it also parse as 5?

    • towerful@programming.dev
      link
      fedilink
      arrow-up
      83
      ·
      1 day ago

      Yup. parseInt is for strings.
      Math.floor, Math.ceil, Math.round or Math.trunc are for numeric type “conversions” (cause its still a float)

      • mmddmm@lemm.ee
        link
        fedilink
        arrow-up
        48
        arrow-down
        4
        ·
        edit-2
        1 day ago

        Nah, it’s stupid either way.

        “5e-7” is not an int to be parsed. Neither is “0.5”.

        • ThirdConsul@lemmy.ml
          link
          fedilink
          arrow-up
          5
          ·
          edit-2
          15 hours ago

          Ah, folly of untyped systems. Tbh this behaviour makes sense given the rules implemented within the language. Anything passed to parseInt is casted to string and then parsed.

          Is it shitty behaviour - yes. Does it make sense in given the language implementation - yes.

          • frezik@midwest.social
            link
            fedilink
            arrow-up
            3
            ·
            edit-2
            9 hours ago

            When handling things that are serialized over the wire, you have to do it this way. Yes, you can use typed serialization formats, but in a string-based serializer, there’s nothing stopping the other system from sending “0.0000005” on a field that should be an int. If you don’t validate that it’s an int, you would just pass that value to your equivalent of parseInt().

            If you do validate that it’s an int, then it still didn’t matter if the language has static typing or not. You’re doing that at runtime or you’re not.

            In Rust, doing "0.00005".to_string().parse::<i32>().unwrap() causes a panic on the unwrap() from an invalid digit. However, that’s runtime. It’s not something the type system can handle statically. The real benefit here, I think, is that it at least forced you to consider that the invalid input could have unexpected results. This is a pretty good reason to be careful about putting unwrap() on everything. The compiler isn’t going to save you here.

          • LarsIsCool@lemmy.world
            link
            fedilink
            arrow-up
            3
            ·
            10 hours ago

            With this reasoning, would you say everything that a computer does makes sense, because it always follows the implementation? Or am I missing something?

        • LeninOnAPrayer@lemm.ee
          link
          fedilink
          English
          arrow-up
          66
          arrow-down
          6
          ·
          edit-2
          1 day ago

          People give JS a lot of shit. And I do too. But it’s meant to continue running and not fail like C code would. It’s meant to basically go “yeah, sure I’ll fuck with that” and keep trucking.

          So you can always make it do stupid shit when you use it a stupid way.

          Is this bad? Maybe. Was it the intention of the language? Absolutely.

          Typescript fixes a lot of these headaches. But I feel like JS is doing exactly what it was meant to do. Keep trucking even when the programmer asks it to do stupid shit.

          If you’re using JS and don’t understand this then it’s your fault and not the languages fault.

          Do we all want to live in a world of typedefs as strict as C and have our webpages crash with the slightest unexpected char input? Probably not.

          We don’t notice all the time JS goes “yeah I can fuck with that” and it works perfectly. We only notice the times it does that and it results in something silly.

          TLDR: JS does what it was made to do. And because of that it looks absolutely ridiculous sometimes.

          • JakenVeina@lemm.ee
            link
            fedilink
            English
            arrow-up
            10
            ·
            19 hours ago

            The REAL problem is that the industry collectively uses JS almost exclusively for shit it was never meant to do. Like you say, it’s intended for it to not throw errors and kill your whole web page, because it was only ever intended to be used for minor scripts inside mostly-static HTML and CSS web pages. Then we all turned it into the most-popular language in the world for building GUI applications.

          • leftytighty@slrpnk.net
            link
            fedilink
            English
            arrow-up
            39
            ·
            1 day ago

            People forget that crashes are a debugging tool indicating an error. Silent errors can be much more dangerous. C and C++ in particular need to be careful not to overwrite random memory for example.

            Yes the consequences for JS failures are less severe and so JS can get away with it, but a crash is a way to know your program isn’t doing what you thought it was, properly.

            It just so happens that JS is used in contexts where nobody really cares, and errors aren’t a big deal, cheap and fast wins.

          • mmddmm@lemm.ee
            link
            fedilink
            arrow-up
            13
            arrow-down
            4
            ·
            1 day ago

            It’s meant to basically go “yeah, sure I’ll fuck with that” and keep trucking.

            Yet, it lives in an insulated environment, with plenty of infrastructure to make sure errors do not propagate, with a standard error handling functionality on the spotlight with specialized syntax, and with plenty of situations where it just drops the ball and throws an error.

            Nope, not falling for the gaslight. It’s a stupid feature that’s there because the language was created during a week and the author was trying to juggle the requirement of a rigid and typed semantics that looked like Java with his desire to make a flexible single-typed language that looks like Lisp.

            And nobody fixed it, decades later, because everybody keeps repeating your line that the interpreter must always keep on.

          • Blackmist@feddit.uk
            link
            fedilink
            English
            arrow-up
            6
            arrow-down
            1
            ·
            1 day ago

            My main issue with JS is you can use it wrong, and it pretends to work, and often looks like it works.

            But then shits its pants explosively the second you fall outside that.

          • wischi@programming.dev
            link
            fedilink
            arrow-up
            7
            arrow-down
            3
            ·
            edit-2
            12 hours ago

            That’s not why JS is a big pile of crap. It’s because the language was not thought through at the beginning (I don’t blame the inventors for that) and because of the web it spread like wildfire and only backwards compatible changes could be made. Even if with all your points in mind the language could be way nicer. My guess is that once wasm/wasi is integrated enough to run websites without JS (dom access, etc.) JS will be like Fortran, Cobol and Telefax - not going away any time soon, but practically obsolete.

  • miellaby@jlai.lu
    link
    fedilink
    arrow-up
    22
    arrow-down
    1
    ·
    1 day ago

    I’ve seen code in my workplace using parseInt to round JS Number. Made me cringe coming from system programing but I didn’t see the danger.

    It’s sad the only way to prevent such a bad code in production is to use transpilers.

  • danda@lemmy.zip
    link
    fedilink
    arrow-up
    100
    ·
    1 day ago

    It’s because parseInt is expecting a string, so the decimal gets converted to a string, and 0.0000005.toString() returns 5e-7.

    • tauonite@lemmy.world
      link
      fedilink
      arrow-up
      19
      arrow-down
      4
      ·
      edit-2
      24 hours 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

        • barsoap@lemm.ee
          link
          fedilink
          arrow-up
          5
          ·
          21 hours 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
      arrow-up
      4
      ·
      18 hours 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().

    • barsoap@lemm.ee
      link
      fedilink
      arrow-up
      14
      ·
      edit-2
      1 day 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.

    • Zacryon@feddit.org
      link
      fedilink
      arrow-up
      10
      arrow-down
      1
      ·
      1 day ago

      I’d advise to always look into the corresponding documentation before using something from any library.

      • pinball_wizard@lemmy.zip
        link
        fedilink
        arrow-up
        5
        arrow-down
        1
        ·
        24 hours ago

        But I’m too busy being confused by the behaviors of libraries I previously didn’t read the documentation for, to read the documentation for every new library I adopt.

        (This is sarcasm…mostly.)

    • jsomae@lemmy.ml
      link
      fedilink
      arrow-up
      6
      arrow-down
      2
      ·
      edit-2
      1 day 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

    • Victor@lemmy.world
      link
      fedilink
      arrow-up
      23
      arrow-down
      18
      ·
      1 day 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.

      • jjjalljs@ttrpg.network
        link
        fedilink
        arrow-up
        38
        arrow-down
        3
        ·
        1 day ago

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

        • Victor@lemmy.world
          link
          fedilink
          arrow-up
          3
          arrow-down
          6
          ·
          edit-2
          1 day 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.

        • heavy@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          4
          arrow-down
          7
          ·
          1 day 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.

      • Traister101@lemmy.today
        link
        fedilink
        arrow-up
        24
        arrow-down
        1
        ·
        1 day ago

        It’s not a string argument though, it’s JS. You can argue it’s expected to be a string but like the rest of JS all you can know from the signature alone is that it takes an object. Hopefully your little ducky quacks the right way!

        • Victor@lemmy.world
          link
          fedilink
          arrow-up
          1
          arrow-down
          2
          ·
          1 day ago

          It’s not a string argument though, it’s JS

          Huh? The code in the image is passing a number argument where there should be a string argument.

          And this function is specifically made to parse a string into an int. Apply common sense.

          • Traister101@lemmy.today
            link
            fedilink
            arrow-up
            1
            arrow-down
            2
            ·
            1 day ago

            JavaScript doesn’t have typed parameters or variables. The function expects a string and does things in the function body which converts the object into a string. JS shares this behavior with all dynamically typed languages and it’s extremely useful in some contexts and extremely frustrating in others. It’s down to what it’s being used for. Dynamic languages make excellent scripting languages, see Python really just being a souped up shell lang

            • Victor@lemmy.world
              link
              fedilink
              arrow-up
              2
              ·
              edit-2
              18 hours ago

              The function expects a string and does things in the function body which converts the object into a string.

              … These are different words that describe exactly what I’m saying. I’m saying: in the place where there should be a string argument, because the function expects one, there is not a string argument, but a number argument. (Not an object like you keep saying.)

              I know all that stuff about dynamically typed languages. I’m just saying that the function is being used incorrectly here.

              • Traister101@lemmy.today
                link
                fedilink
                arrow-up
                1
                arrow-down
                1
                ·
                1 day ago

                You cannot have a string argument, arguments and variables in JS don’t have a type. All you have in JS is objects. Actual functions, like full on function foo(){} are still objects, like you can actually store data on the things.

                • Victor@lemmy.world
                  link
                  fedilink
                  arrow-up
                  2
                  arrow-down
                  1
                  ·
                  edit-2
                  18 hours ago

                  I think you confuse argument with parameter. You cannot specify the type of the parameter, but any argument you supply to a function in JS has a type. Every value in JS has a type, arguments included.

                  If I go:

                  const n = 0.0000005;
                  console.log(typeof n);
                  

                  The code above will print “number”. And you cannot assign n.foo = "metadata"; to this value of a primitive type. Not everything is an object.

                  Either way, arguments have types, values have types. The arguments in this case were of type “number”, when they should have been “string”.

      • qqq@lemmy.world
        link
        fedilink
        arrow-up
        13
        ·
        edit-2
        1 day 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@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          1 day 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. ❤️

      • Malix@sopuli.xyz
        link
        fedilink
        arrow-up
        2
        arrow-down
        1
        ·
        1 day ago

        What do you mean, you don’t use string parsing method to round to integers? /s