parseInt() not only parses but also truncates! is it okay?
#JavaScript has two global methods named: parseFloat() and parseInt(). On the other hand there is only one data type to represent numbers in JS: "Number".
Technically, parseInt is just the same as parseFloat except that it also truncates the given number in case it includes some decimal part.
The point is that the behavior of parseInt is not actually parsing a string containing int! in fact it is parsing a float and then processing the result. I think there is a bad semantic gap between the meaning of the term used for that method and what it actually does. That may reduce readability of your code and misguide programmers reviewing your code, specially in case they are not completely familiar with JS.
Maybe ECMAScript has better drop those methods and introduce just a single parseNumber() method! and leave rounding to developers themselves!
function parseNumber(str) { return parseFloat(str); // may return NaN, which should be handled by the caller! } function dropDecimalPart(num) { return Math.trunc(num);
}
cover photo: by Fatos Bytyqi on Unsplash
#cs_internship #web