DEV Community

Discussion on: You should stop using `parseInt()`

Collapse
 
andreidascalu profile image
Andrei Dascalu

The most common case for ParseInt is to get the correct number from a string with the even expectation of getting a float .... from a function that has int in the name? I hardly believe that.
I have never seen in 20 years a case of parseInt used with the expectation of getting anything except an int.

Thread Thread
 
darkmavis1980 profile image
Alessio Michelini

Unfortunately I see it using it for what Number is supposed to do, many, many times.
Btw, if you just need to get the integer (that is a positive number) from a string containing a float, then you should use double tilde operator, which does the same as Math.floor, but just 10 times faster (in Node at least), for example:

console.log(~~'3.2'); // returns 3
console.log(parseInt('3.2')); // returns 3
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
markgoho profile image
Mark Goho

double tilde...🤯