210
parseInt(5) (lemmy.ml)
top 24 comments
sorted by: hot top controversial new old
[-] heavy@sh.itjust.works 12 points 1 hour ago

I know this is for fun, but as general advice to the homies, if a language or system is doing something you didn't expect, make sure to look at the documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

This will save a lot of time and headaches

[-] Zacryon@feddit.org 2 points 59 minutes ago

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

[-] m_f@discuss.online 117 points 4 hours ago

If anyone's wondering why:

>> 0.000005
0.000005
>> 0.0000005
5e-7
[-] towerful@programming.dev 22 points 3 hours 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 16 points 2 hours ago* (last edited 2 hours ago)

Nah, it's stupid either way.

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

[-] LeninOnAPrayer@lemm.ee 13 points 1 hour ago* (last edited 1 hour 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.

[-] leftytighty@slrpnk.net 7 points 1 hour 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.

[-] wischi@programming.dev 3 points 53 minutes 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 will 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.

[-] ICastFist@programming.dev 4 points 1 hour ago

Javascript: "They're the same thing"

[-] danda@lemmy.zip 76 points 5 hours ago

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

[-] spizzat2@lemm.ee 15 points 3 hours ago* (last edited 2 hours ago)

And to further expand on that, if you do pass in a ~~sting~~ string, it handles it correctly.

> parseInt('0.0000005')

0
[-] SpaceNoodle@lemmy.world 8 points 2 hours ago

What if I pass in a Sterwart Copeland?

[-] spizzat2@lemm.ee 4 points 2 hours ago

๐Ÿ˜† I'll be watching you...

[-] KindaABigDyl@programming.dev 29 points 4 hours ago

Common Dynamic Typing L

[-] FMT99@lemmy.world 40 points 4 hours ago

Another classic javascript wat

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

[-] Traister101@lemmy.today 15 points 2 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!

[-] jjjalljs@ttrpg.network 18 points 2 hours 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 -1 points 1 hour 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 12 points 1 hour ago

Maybe the design is bad, then.

[-] heavy@sh.itjust.works 3 points 1 hour ago

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

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

[-] Malix@sopuli.xyz 0 points 2 hours ago

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

[-] enemenemu@lemm.ee 8 points 4 hours ago

Great feature

this post was submitted on 05 May 2025
210 points (99.5% liked)

Programmer Humor

22995 readers
882 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