Oh JavaScript... This is why we don't get along.

Jan 27, 2024 · 10:33 PM UTC

121
170
2,353
745,006
RepliesRelevant
Replying to @eloffd
@eloffd parseInt is for strings, so it is trying to do parseInt('5e-7'), which is 5. In this case, it is recommended to use: Math.floor
11
10
478
24,915
Replying to @yortyrh
@yortyrh Yep, that's right
1
1
36
22,654
Replying to @eloffd
@eloffd Are you from the Typescript sales department, sir?
1
0
1
350
Replying to @garyfung
@garyfung I should be, the amount of times I plug it on here, they need to put me on commission
0
0
1
304
Replying to @eloffd
@eloffd I hate parseInt 😭😭 I forget this at least once a year and I'm like... TF
1
0
1
1,982
Replying to @specialkdelslay
@keyladelslay I’m not going to lie, if I don’t use typescript I also make this mistake
0
0
2
1,732
Replying to @eloffd
@eloffd “Parse int is for strings”

Doesn’t matter - this makes no lexical sense so it’s not something that should be accepted.
3
1
96
16,024
Replying to @JamesRLandrum
@JamesRLandrum It should be a type error
9
0
152
14,868
Replying to @eloffd
@eloffd This broke me:
[1, 9, 84, 2].sort()
> [1, 2, 84, 9]
5
6
66
18,784
Replying to @FnH
@FnH How does this even work?

"The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values."

That's INSANE. Still I don't see how it gets that result?
3
1
30
14,794
Replying to @eloffd
@eloffd This is forcing the square peg into the round hole, and crying it broke eventually.
1
0
34
3,871
Replying to @TheMeowku
@TheMeowku The round hole should throw an error when it gets a square peg. It shouldn't put in on a lathe to try to carve off the edges.
2
0
25
3,487
Replying to @eloffd
@eloffd It's funny as well as scary that the whole world directly or indirectly depends on such a bad language.
1
0
18
5,445
Replying to @ankk98
@ankk98 All built on the sand
1
0
4
4,742
Replying to @eloffd
@eloffd This is totally correct: parseInt parses a string, discarding any non-integer rest, and 0.0000005 rendered as a standard string "5e-7". So, what is the leading string? Well, 5.
However, parsing an actual string by 'parsInt("0.0000005") gives you 0.
(For numbers use Math.floor!)
1
1
9
2,678
Replying to @mass_werk
@mass_werk Correct, but such poor design at the same time!
2
1
7
2,077