What is the advantage of using Number() instead of parseInt or parseFloat?
Number() is both more flexible and strict. Number can interpret a value in scientific notation (“7e2” = 700), but will NaN anything that isnt actually a number format (“80px” = NaN), whereas parseInt just reads characters until it finds a non-digit and stops. (“7e2” = 7; “80px” = 80).
Number will also lead to odd situations that parseInt doesnt; it will interpret empty-ish values (null, false, “”, etc) as 0’s, and true
as 1. (parseInt returns NaN for all of those)
One somewhat nice thing is that you can shorthand Numbers, because Javascript defaults to using Number when doing unspecified string to numbertype conversions:
const tdValue = Number(tdElement.dataset.val) || 0;
could be written as
const tdValue = +tdElement.dataset.val || 0;
Ok,
for me that means: If I am sure I need an decimal integer I will still use parseInt() as strings like “0x22” will not be parsed.
mmh no, that’s the opposite. parseInt will happily parse “0x22” and tell you the answer is 0. Number will parse 0x22 and tell you the answer is 34 (which it is.) [You may have accidentally given an actual numerical value string in that post?]
No, as 0x22 is no decimal integer it is a hexadecimal number.
Correct… so Number will actually give you the value, and parseInt will give you the wrong value. shrug
I think we are talking of different things. If I need my string to be a decimal integer, I do not want to have a result if the string contains something like “0x22” as this string is not a decimal integer but a hexadecimal number.
Then neither parseInt or Number will work for you in that situation; both will return a result, because the first character in the string is a digit.
You’ll have to be more selective of your strings if you want to avoid that. Number will take a number-formatted string, and turn it into a numerical value of the appropriate type. parseInt will take any string that begins with a digit, and turn it into a flatform integer.
Ok, you are right. To make it more precise.
If I need my string to be a not 0 decimal integer value
Then yes, parseInt will work in the case of hex value strings, but only because they start with 0.
If you give it a scientific value, “3.29e10”, parseInt will tell you its 3
. But you wanted your string to be decimal integer and not 0…
You still havent got a decimal string, but you’ve got a non-zero value. The wrong, non-zero value.
Ok, true. So there is no other way to determine if a string contains a decimal integer than doing a test() with regex?
You could parseInt it, convert back to a string, and compare string lengths?
let in1 = "1239";
let int1 = parseInt(in1);
int1.toString().length == in1.length //true
let in2 = "123.9";
let int2 = parseInt(in1); //123
int2.toString().length == in2.length //false
Wouldnt work in ALL circumstances (if you happened to push a hex that had the same length in decimal as hex) but…
Regex would be the best solution.
You see, I am a very old programmer and when I started to develop software, I had to take care of the performance of my code all over the time.
This is something which I cannot stop in my head.
And if I see, that functions like Number() or Regex which, in case of their complexity, are hundred times slower then parseInt() or simply comparing a string, are used as a standard even if it is not used too in 99% of the cases I always struggle with this solution.
Benchmark: parseInt vs Number addition - MeasureThat.net
A quick run through that says that parseInt is slower? I cant swear to the results though…
I’d much rather side with Number() for readability of the code. Performance differences are not relevant until we might save a total of half a second or more when doing thousands of those same operations at the same time, which is a situation that has not occurred yet in all my years of programming.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.