In Javascript, we have numerous ways to check if something is or is not a number. This is a particularly common task in Javascript, where there is dynamic typing, resulting in some unexpected things being classified as numbers. fixes some of these issues, but in this guide, we'll cover how to check if something is a number in Javascript, and the pitfalls you should avoid when trying to do that. Typescript Introducing isNaN is a special value in Javascript, which stands for "Not a Number". If you try to parse a text string in Javascript as an , you'll get NaN: NaN int let x = parseInt("hello") // Returns NaN in itself is kind of confusing, and you don't always get the results you would expect. , for example, does not equal any other value, including itself. Testing this out will always return false: NaN NaN 5 === NaN // false NaN === NaN // false "foo" === NaN // false You might think this all makes sense until you try to run - which returns . So it turns out that is of type 'number' in Javascript after all: typeof NaN number NaN typeof NaN // 'number' Ignoring these peculiarities, Javascript comes with a built-in function to test if something is "not a number" known as . This function can easily be used to determine if a something would evaluate to if it was run through something like : isNaN NaN parseFloat isNaN("hello") // true isNaN(5) // false isNaN({}) // true isNaN(() => {}) // true Since checks if something is not a number, we can use to test if something is a number. For example, is an easy way to test if is a number: isNaN !isNaN !isNaN(5) 5 !isNaN(5) makes sense in most cases, but since it parses numbers, it can cause some unexpected side effects. For example, on types throws an error, and therefore throws an error on too: isNaN Number(1n) BigInt isNaN isNaN(1n) // true To resolve some of these problems, Javascript just made a new method, called . It's mostly the same, only it won't coerce the type to a number. Number.isNaN Number.isNaN vs isNaN They are commonly thought to be the same, but and work differently. essentially parses the input, and tries to make a number out of it. That's why you see problems when you try to do , since throws an error. Instead, you can use isNaN Number.isNaN isNaN isNaN(1n) Number(1n) Number.isNaN() The difference between and is that does not try to coerce the input into a number. Unlike , it simply takes the input and confirms if it is equal to or not. That makes isNaN Number.isNaN Number.isNaN isNaN NaN So all of the following will return false, since none of them are exactly equal to : NaN Number.isNaN({}) // false Number.isNaN("hello") // false Number.isNaN(() => {}) // false Number.isNaN("5") // false while the following will return true, since they do return : NaN Number.isNaN(5 / "5") // true Number.isNaN(parseFloat("hello")) // true Either or will solve most of your number-checking needs, but there is one additional way to check if something is a number in Javascript Number.isNaN isNaN Using isInteger and isSafeInteger to check a number in Javascript As well as and , the global methods and can help you determine if something is simply an integer, with no decimal points. Just like , both of these methods do not try to evaluate the contents as a number. That means passing in a string will always return false, while a normal integer will pass the test: isNaN Number.isNaN isInteger isSafeInteger Number.isNaN isInteger("5") // false isInteger(5) // true isSafeInteger("5") // false isSafeInteger(5) // true differs from by checking that the number falls outside the range - i.e. within and - so for most use cases will do the job. isSafeInteger isInteger bigint -2^53 2^53 isInteger Using typeof to check if something is a number in Javascript The final way to check if something is a number is to use - again, this may fit your needs better for some cases, since returns , rather than - however things like will still show a type of : typeof typeof Math.sqrt(-1) number NaN 1n bigint typeof Math.sqrt(-1) // 'number' typeof parseFloat("35") // 'number' typeof 35 // 'number' typeof 1n // 'bigint' However, since it is quite unreliable. Since returns , you can run into some unexpected situations which you will generally want to avoid. As such, remains probably the best way to check if something is or isn't a number. be careful typeof NaN number Number.isNaN Here are a few unexpected situations you'll generally want to avoid: typeof typeof parseFloat("hello") // 'number' - since NaN is a number typeof 5 / "5" // 'NaN' - since this evaluates typeof 5, and then divides by "5" typeof (5 / "5") // 'number' - since this evaluates as NaN, which is a number typeof NaN // 'number' - since NaN is of type number typeof "5" // 'string' Conclusion Checking if something is or is not a number in Javascript has some complexities, but it's generally straightforward. The key points are: is commonly used, but will evaluate its input as a number, which may cause some inputs are incorrectly judged to be or throw an error. isNaN NaN is a robust version of , which checks if something is exactly equal to . It does not evaluate its contents as a number Number.isNaN isNaN NaN can tell you if something is a or not, but it may lead to some unexpected situations, since is also of type number. typeof number NaN I hope you've enjoyed this guide on checking if something is a number in Javascript. . You can also check out more of my Javascript content here Also Published Here