How to Check if a Value is a Number in JavaScript

avatar

Borislav Hadzhiev

Sun Oct 31 20213 min read

Check if a Value is a Number #

You can check if a value is a number in three ways:

  1. typeof - if the value is a number, the string "number" is returned.
  2. Number.isFinite() - if the value is a number, true is returned.
  3. isNaN() - if the value is a number, false is returned.

Use the typeof operator to check if a value is a number in JavaScript, e.g. typeof myVar === 'number'. The typeof operator returns a string that indicates the type of the value. For numbers, it returns the string - "number".

index.js
const myVar = 5; if (typeof myVar === 'number') { console.log('โœ… value is a number'); } else { console.log('โ›”๏ธ value is NOT a number'); }

We used the typeof operator to check if a value is a number.

The typeof operator returns a string that indicates the type of a value. Here are some examples.

index.js
console.log(typeof 5); // ๐Ÿ‘‰๏ธ "number" console.log(typeof '5'); // ๐Ÿ‘‰๏ธ "string" console.log(typeof NaN); // ๐Ÿ‘‰๏ธ "number" console.log(typeof {}); // ๐Ÿ‘‰๏ธ "object" console.log(typeof []); // ๐Ÿ‘‰๏ธ "object" console.log(typeof null); // ๐Ÿ‘‰๏ธ "object"

If you look closely, you'll notice that NaN (Not a Number) is also of type number.

To make sure we don't get any NaN values to pass our conditional check, let's add another condition.

index.js
const myVar = 5; if (typeof myVar === 'number' && !Number.isNaN(myVar)) { console.log('โœ… value is a number'); } else { console.log('โ›”๏ธ value is NOT a number'); }

We used the logical AND (&&) operator to chain a second condition. Both of the conditions have to return a truthy value for our if block to run.

Notice that we used the logical NOT (!) operator to negate the call to Number.isNaN. This takes the return value from the method, inverts it and converts it to boolean.

Now we also check if the Number.isNaN method returns false. The Number.isNaN method determines if the passed in value is NaN (not a number) AND it's type is number.

Here are some examples:

index.js
console.log(Number.isNaN(NaN)); // ๐Ÿ‘‰๏ธ true console.log(Number.isNaN(Number.NaN)); // ๐Ÿ‘‰๏ธ true console.log(Number.isNaN(0 / 0)); // ๐Ÿ‘‰๏ธ true console.log(Number.isNaN(5)); // ๐Ÿ‘‰๏ธ false console.log(Number.isNaN('5')); // ๐Ÿ‘‰๏ธ false console.log(Number.isNaN({})); // ๐Ÿ‘‰๏ธ false

Only NaN, Number.NaN and 0 / 0 return true.

If we know that the value has a type of number and it's not NaN, we can conclude that it's a valid number.

Check if a Value is a Number using Number.isFinite #

To check if a value is a number, pass the value to the Number.isFinite() method, e.g. Number.isFinite(myVar). The method returns true if the provided value is of type number, is not positive or negative Infinity, nor NaN.

index.js
const myVar = 5; if (Number.isFinite(myVar)) { console.log('โœ… value is a number'); } else { console.log('โ›”๏ธ value is NOT a number'); }

We used the Number.isFinite method to combine the checks from the previous example.

The Number.isFinite() method checks that the provided value:

  • is of type number
  • is not positive or negative Infinity
  • is not NaN

Here are some examples:

index.js
console.log(Number.isFinite(5)); // ๐Ÿ‘‰๏ธ true console.log(Number.isFinite(5.5)); // ๐Ÿ‘‰๏ธ true console.log(Number.isFinite('5')); // ๐Ÿ‘‰๏ธ false console.log(Number.isFinite(NaN)); // ๐Ÿ‘‰๏ธ false console.log(Number.isFinite(null)); // ๐Ÿ‘‰๏ธ false
The Number.isFinite method is not as commonly used as the typeof operator. If you use it in your code, you might surprise quite a lot of developers.

Check if a Value is a Number using NaN #

There are 2 isNaN methods in JavaScript:

  • Number.isNaN() - checks if the passed in value is of type number and is NaN (no coercion)
  • isNaN() - coerces the value to a number and checks if the value is NaN
You might see examples using this approach, but you should never use it, because of the examples that follow.
index.js
// โ›”๏ธ Don't do this const myVar = 5; if (!isNaN(myVar)) { console.log('value might be a number'); } else { console.log('value is NOT a number'); }

We negated a call to the isNaN() function to check if the passed in value can be coerced to a number.

Here are some examples of using isNaN.

index.js
console.log(isNaN(5)); // ๐Ÿ‘‰๏ธ false console.log(isNaN('5')); // ๐Ÿ‘‰๏ธ false console.log(isNaN(3.5)); // ๐Ÿ‘‰๏ธ false console.log(isNaN('3.5')); // ๐Ÿ‘‰๏ธ false console.log(isNaN('3.5abc')); // ๐Ÿ‘‰๏ธ true console.log(isNaN({})); // ๐Ÿ‘‰๏ธ true console.log(isNaN([])); // ๐Ÿ‘‰๏ธ false console.log(isNaN(true)); // ๐Ÿ‘‰๏ธ false console.log(isNaN(false)); // ๐Ÿ‘‰๏ธ false console.log(isNaN(null)); // ๐Ÿ‘‰๏ธ false console.log(isNaN(undefined)); // ๐Ÿ‘‰๏ธ true

If the passed in value is not of type number, the isNaN function converts the value to a number before checking if it's NaN.

This goes wrong is some many cases, because many non numeric values convert to a valid number. Here are some examples:

index.js
console.log(Number(null)); // ๐Ÿ‘‰๏ธ 0 console.log(Number([])); // ๐Ÿ‘‰๏ธ 0 console.log(Number('')); // ๐Ÿ‘‰๏ธ 0 console.log(Number(true)); // ๐Ÿ‘‰๏ธ 1 console.log(Number(false)); // ๐Ÿ‘‰๏ธ 0

For any of these values, you would get a false positive.

You should never use this approach, as it leads to confusing behavior and difficult to track bugs.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee