399

Is there is any function like isNumeric in pure JavaScript?

I know jQuery has this function to check the integers.

3

6 Answers 6

675

There's no isNumeric() type of function, but you could add your own:

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

NOTE: Since parseInt() is not a proper way to check for numeric it should NOT be used.

18
  • 19
    This solution is used in / taken from Jquery library $.isNumeric(obj) api.jquery.com/jquery.isnumeric
    – ThdK
    Commented Jan 12, 2015 at 12:12
  • 2
    @Matt My comment was aimed at the comment and the note in the answer stating parseInt was the wrong way of doing this (then going ahead and using parseFloat, which doesn't really seem different). Interestingly isFinite will get you the result you're after in almost all cases on its own (apart from whitespace strings, which for some reason return true), so parseInt vs parseFloat seems irrelevant in this context. I certainly can't find a single input to that function where parseInt makes a difference.
    – Vala
    Commented Aug 17, 2015 at 20:10
  • 3
    DON'T USE THIS FUNCTION it does not work with floats. See jQuery's isNumeric here for ideas on implementing it.
    – tsiege
    Commented Sep 3, 2015 at 19:03
  • 3
    What about Number.isInteger()? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – kurdtpage
    Commented Aug 13, 2019 at 3:55
  • 6
    Careful. This function returns true for single element arrays containing a numeric element: isNumeric([0]) === true and isNumeric(['123']) === true Commented Dec 12, 2019 at 20:11
210

This should help:

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

Very good link: Validate decimal numbers in JavaScript - IsNumeric()

0
109
function IsNumeric(val) {
    return Number(parseFloat(val)) == val;
}
10
  • 7
    I think the updated version of this is actually the best answer here. I find it odd that it has the least up-votes. I thought at first that it might fail if IsNumeric was called with NaN, but due to the quirk of NaN that it's not equal to anything it actually works out fine.
    – Vala
    Commented Dec 4, 2014 at 11:31
  • 4
    And I think you can also drop the cast to Number() and rely on the double equals to do a bit of conversion: let isNumeric = (val) => parseFloat(val) == val; Commented Aug 31, 2016 at 11:40
  • As per my requirement which I believe many other developers would have it should be function IsNumeric(val) { return Number(val)==val; } So that it ignores in case of empty string. Because I only want to validate if there is invalid value entered. While allowing empty string which is not number. I think all three answers are good based on scenarios Commented Sep 1, 2016 at 22:40
  • Explain the code please. Why not Number(val).toString() == val.toString()?
    – x-yuri
    Commented Apr 15, 2018 at 0:11
  • 1
    if Val is undefined then .ToString will throw exception Commented May 10, 2018 at 15:12
22

There is Javascript function isNaN which will do that.

isNaN(90)
=>false

so you can check numeric by

!isNaN(90)
=>true
5
  • 36
    except for "" and true and false which are not-not-a-number ...
    – commonpike
    Commented Nov 30, 2012 at 16:17
  • 6
    I get isNaN(null) === false
    – jayflo
    Commented Jun 8, 2016 at 21:50
  • 1
    isNaN("null") === true; isNaN(null) === false; for me too
    – cmeza
    Commented Feb 14, 2018 at 16:14
  • 2
    isNaN(true) === false; isNaN(false) === false;
    – cmeza
    Commented Feb 14, 2018 at 16:16
  • console.log(isNaN('34.234,23')); // true :-( Commented Oct 16, 2023 at 20:12
19
var str = 'test343',
    isNumeric = /^[-+]?(\d+|\d+\.\d*|\d*\.\d+)$/;

isNumeric.test(str);
5
  • 6
    Well, depending on the use case, it might actually be the worst answer here. For example, this returns false against ' 1' or '0x01', localization is not taken into account...
    – Eric Redon
    Commented Jul 25, 2014 at 20:49
  • 1
    I kinda like this answer better, if you are reading chunks from a string or an user input, isNaN and parseInt can result in unwanted false positives like "123abc", "2e1", "0x2", etc.. (even jQuery.isNumeric will parse true). I would use something like this function isNumeric(str) { return /^\d*\.{0,1}\d*$/.test(str); }
    – ebob
    Commented May 1, 2017 at 19:19
  • 3
    Well, depending on the use case, it might actually be the best answer here. Commented Dec 20, 2017 at 19:19
  • As long as you trim strings and aren't worried about alternative representations this is a reasonable answer. Commented May 15, 2019 at 18:37
  • You didn't cater for Locale. Thousands and Decimal separators can be other way around in some countries. Commented Oct 16, 2023 at 19:33
12

isFinite(String(n)) returns true for n=0 or '0', '1.1' or 1.1,

but false for '1 dog' or '1,2,3,4', +- Infinity and any NaN values.

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.