How to Check if a Value is a Number in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
5 min

banner

# Check if a Value is a Number in JavaScript

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.

# Check if a Value is a Number using the typeof operator

Use the typeof operator to check if a value is a number in JavaScript.

The typeof operator will return the string "number" if the value is of type number.

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

check if value is number using typeof operator

The code for this article is available on GitHub

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.

Use the Number.isNaN() method to make sure NaN values don't meet the condition.

index.js
const myVar = 5; if (typeof myVar === 'number' && !Number.isNaN(myVar)) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… value is a number'); } else { console.log('โ›”๏ธ value is NOT a number'); }
The code for this article is available on GitHub

We used the logical AND (&&) operator to chain a second condition.

Both conditions have to be met for our if block to run.

Notice that we used the logical NOT (!) operator to negate the call to Number.isNaN method.

The NOT (!) operator takes the return value of the method, converts it to a boolean and then flips it.

The Number.isNaN method checks if the passed-in value is NaN (not a number) AND its 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, then it is a valid number.

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

Alternatively, you can use the Number.isFinite().

The method returns true if the provided value is of type number and is not positive or negative Infinity or NaN.

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

check if value is number using number isfinite

The code for this article is available on GitHub

We used the Number.isFinite() method to combine the conditions 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 that use this approach, but you should never use it because of the following caveats.
index.js
// โ›”๏ธ Don't do this const myVar = 5; if (!isNaN(myVar)) { // ๐Ÿ‘‡๏ธ this runs console.log('value might be a number'); } else { console.log('value is NOT a number'); }
The code for this article is available on GitHub

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 in many cases because many non-numeric values convert to valid numbers. 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.

# Check if a Variable is a Number using lodash

First, make sure you have lodash installed by running the following command from your terminal.

shell
# ๐Ÿ‘‡๏ธ initialize package.json if you don't have one npm init -y npm install lodash

Now you can import and use the isNumber method to check if a value is a number.

index.js
import _ from 'lodash'; console.log(_.isNumber(NaN)); // ๐Ÿ‘‰๏ธ true console.log(_.isNumber(5)); // ๐Ÿ‘‰๏ธ true console.log(_.isNumber(-100)); // ๐Ÿ‘‰๏ธ true console.log(_.isNumber('hello')); // ๐Ÿ‘‰๏ธ false console.log(_.isNumber(null)); // ๐Ÿ‘‰๏ธ false console.log(_.isNumber([])); // ๐Ÿ‘‰๏ธ false const variable = 100; if (_.isNumber(variable)) { // ๐Ÿ‘‡๏ธ this runs console.log('The value is a number'); } else { console.log('The value is NOT a number'); }

check if variable is number using lodash

The code for this article is available on GitHub

The lodash.isNumber() method returns true if the supplied value is a number and false otherwise.

Even if you already use lodash in your project, I would advise against using the isNumber() method as the typeof operator is sufficient and doesn't require you to use an external library.

# Don't use the instanceof operator to check if a value is a number

You should never use the instanceof operator to check if a value is of type number.

index.js
import _ from 'lodash'; function isNumber(value) { return value instanceof Number; } console.log(isNumber(100)); // ๐Ÿ‘‰๏ธ false console.log(isNumber(-1000)); // ๐Ÿ‘‰๏ธ false console.log(isNumber(new Number(10))); // ๐Ÿ‘‰๏ธ true console.log(isNumber(new Number(-100))); // ๐Ÿ‘‰๏ธ true console.log(isNumber(null)); // ๐Ÿ‘‰๏ธ false console.log(isNumber('')); // ๐Ÿ‘‰๏ธ false
The code for this article is available on GitHub

The syntax for the instanceof operator is object instanceof constructor.

The operator returns true if the prototype property of the constructor appears anywhere in the prototype chain of the object.

Notice that the operator returns true only for object-wrapped numbers.

These are numbers that were created with the new Number() constructor.

You should never use the new Number() constructor in your code, so you should never have to use the instanceof operator to check if a value is a number.

The typeof operator is all you need in order to check if a value stores a number in JavaScript.

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

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev