3

I read online tutorial it states: Actually, the most reliable conversion check is either a regexp or isNumeric below:

function isNumeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n)
}

My question is why this function can not be simplified to:

  function isNumeric(n) {
      return isFinite(n)
  }
5
  • is it not required because if n is undefined it will result in a reference error Commented Oct 23, 2014 at 11:00
  • So it can be simplified right? I find the tutorial is on javascript.info/tutorial/number-math Commented Oct 23, 2014 at 11:04
  • @Niko ah I misunderstood the problem (just thought check the input as number). Commented Oct 23, 2014 at 11:11
  • @bean You should maybe include some kind of input->output specification in your question to make clear for what exactly your function should test. Commented Oct 23, 2014 at 11:16
  • @Niko thanks, I will add them next time. Commented Oct 23, 2014 at 11:18

3 Answers 3

2

My question is why this function can not be simplified to:

function isNumeric(n) {
   return isFinite(n)
}

It can't, no, you want that parseFloat in there if there's any chance n may not be a number to start with. But it could be simplified to:

function isNumeric(n) {
    return isFinite(parseFloat(n));
}

...because isFinite(NaN) is false, which is what your function returned for unparseable numbers anyway.

It's worth noting that parseFloat will stop at the first invalid character and return whatever number it found up to that point, so parseFloat("123abc") is 123, not NaN. If you want to validate that the entire string can be correctly parsed as floating point, use + or Number():

function isNumeric(n) {
    return isFinite(+n); // Or: return isFinite(Number(n));
}

Now, if you know for sure that n will always already be a number (not a boolean or a string or something), then yes, you could skip the parsing stage.

Sign up to request clarification or add additional context in comments.

Comments

2

Because isFinite(n) will return true for non-numeric values, for example:

alert(isFinite(false)); // displays true

2 Comments

NaN is a non-numeric value but isFinite(NaN) returns false.
It's maybe just that the word "for some non-numeric values" is missing here (as there only needs to be a single legal input that leads to the wrong output to invalidate the approach).
0

Well no you can't use is finite as that will only return true for a finite number.

For example isFinite(1/0) returns false.

Infinity is a number its just not finite.

I'd suggest looking at how the big libraries do it for example:

Jquery

isNumeric: function( obj ) {
// parseFloat NaNs numeric-cast false positives (null|true|false|"")
// ...but misinterprets leading-number strings, particularly hex literals ("0x...")
// subtraction forces infinities to NaN
   return obj - parseFloat( obj ) >= 0; 
}

Underscore.js

function isNumber(obj) {
   return toString.call(obj) == '[object Number]';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.