326
parseInt(5) (lemmy.ml)
you are viewing a single comment's thread
view the rest of the comments
[-] FMT99@lemmy.world 43 points 10 hours ago

Another classic javascript wat

[-] victorz@lemmy.world 3 points 8 hours 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 26 points 8 hours ago

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

[-] victorz@lemmy.world 1 points 5 hours ago* (last edited 5 hours 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 -2 points 7 hours 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.

[-] jjjalljs@ttrpg.network 20 points 7 hours ago

Maybe the design is bad, then.

[-] heavy@sh.itjust.works 3 points 6 hours ago

Lol you'll get no argument from me. It's not my favorite language.

[-] bleistift2@sopuli.xyz 0 points 4 hours ago

JavaScript has types and it does have type errors, for instance

> null.foo
Uncaught TypeError: null has no properties

Please stop spouting nonsense on issues you know nothing about.

[-] heavy@sh.itjust.works 0 points 4 hours ago

Dynamic types aren't static types my man. I think you got some learning to do.

[-] zqwzzle@lemmy.ca -1 points 5 hours ago

Theoretically, Javascript is an untyped language…

Function only handles string arguments correctly. Wat.

[-] Traister101@lemmy.today 19 points 8 hours 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!

[-] victorz@lemmy.world -2 points 5 hours 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 0 points 4 hours 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

[-] victorz@lemmy.world 1 points 2 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 indirectly here.

[-] Traister101@lemmy.today 1 points 1 hour 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.

[-] qqq@lemmy.world 13 points 8 hours ago* (last edited 8 hours 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

[-] victorz@lemmy.world 1 points 5 hours 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 1 points 8 hours ago

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

[-] victorz@lemmy.world 1 points 5 hours ago
this post was submitted on 05 May 2025
326 points (99.1% liked)

Programmer Humor

23025 readers
1408 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS