this post was submitted on 05 May 2025
485 points (98.8% liked)

Programmer Humor

23082 readers
2740 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
 
you are viewing a single comment's thread
view the rest of the comments
[–] FMT99@lemmy.world 51 points 2 days ago (1 children)

Another classic javascript wat

[–] victorz@lemmy.world 5 points 2 days ago (4 children)

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 36 points 2 days ago (2 children)

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 -3 points 2 days ago* (last edited 2 days ago) (1 children)

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.

[–] listless@lemmy.cringecollective.io 2 points 2 days ago (1 children)

And god fucking forbid that common sense be in the language. Who the fuck needs a language with common sense, amirite?

[–] heavy@sh.itjust.works -3 points 2 days ago (3 children)

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 28 points 2 days ago (1 children)

Maybe the design is bad, then.

[–] heavy@sh.itjust.works 5 points 2 days ago

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

[–] bleistift2@sopuli.xyz 6 points 2 days ago (1 children)

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 -3 points 2 days ago (1 children)

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

[–] victorz@lemmy.world 5 points 2 days ago (1 children)
[–] heavy@sh.itjust.works 0 points 1 day ago

Lol like facilitate versus effectuate

[–] zqwzzle@lemmy.ca -2 points 2 days ago

Theoretically, Javascript is an untyped language…

Function only handles string arguments correctly. Wat.

[–] Traister101 23 points 2 days ago (2 children)

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!

[–] hedgehog@ttrpg.network 1 points 2 days ago (1 children)

Where are you getting these typeless function signatures? You should get them from somewhere else.

The Ecmascript Spec for parseInt doesn’t have a type listed for the first argument, but it does call the argument “string,” which should be a pretty clear hint that it’s expected to be a string. It also explicitly states that the first step of parseInt is to coerce the first argument to a string. You should view older versions of the spec to see how it performs in older browsers / runtimes.

The MDN entry for parseInt makes it clear that parseInt expects a string or a string and explicit radix (i.e., 10 for base 10/decimal, or 16 for hexadecimal). MDN is what I recommend anyone who’s writing JavaScript use as their first resource for core JS features, for what that’s worth.

I use WebStorm to write JavaScript (and more frequently, TypeScript), and if I type parseInt( and pause a second, it pops up a type hint that reads string: string, radix?: number.

I checked in VSCode, too, and it pops up this:

parseInt(string: string, radix?: number | undefined): number`
A string to convert into a number.
Converts a string to an integer.

You can set something similar up with an LSP in Vim or Neovim, and presumably in Emacs as well.

[–] Traister101 1 points 2 days ago

You have argued that it's expected to be a string congrats. You'll notice from the grammer you used that there isn't an actual type here, you specifically pointed out how your IDE displays a type hint which is only slightly better than somebody writing some form of documentation on what the type is. Dynamic languages don't have typed args, least none that I've heard of.

[–] victorz@lemmy.world 0 points 2 days ago (1 children)

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 -1 points 2 days ago (1 children)

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 2 points 2 days ago* (last edited 2 days ago) (1 children)

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 0 points 2 days ago (1 children)

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.

[–] victorz@lemmy.world 1 points 2 days ago* (last edited 2 days 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 14 points 2 days ago* (last edited 2 days ago) (1 children)

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 2 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. ❤️

[–] Malix@sopuli.xyz 1 points 2 days ago (1 children)

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

[–] victorz@lemmy.world 1 points 2 days ago