Last updated: Mar 4, 2024
Reading timeยท5 min
You can check if a value is a number in three ways:
typeof
- if the value is a number, the string "number"
is returned.Number.isFinite()
- if the value is a number, true
is returned.isNaN()
- if the value is a number, false
is returned.typeof
operatorUse 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.
const myVar = 5; if (typeof myVar === 'number') { // ๐๏ธ this runs 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.
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.
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'); }
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:
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
.
number
and it's not NaN
, then it is a valid number.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
.
const myVar = 5; if (Number.isFinite(myVar)) { // ๐๏ธ this runs console.log('โ value is a number'); } else { console.log('โ๏ธ value is NOT a number'); }
We used the Number.isFinite() method to combine the conditions from the previous example.
The Number.isFinite()
method checks that the provided value:
number
Infinity
NaN
Here are some examples:
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
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.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
// โ๏ธ 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'); }
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
.
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
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:
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.
lodash
First, make sure you have lodash
installed by running the following command
from your terminal.
# ๐๏ธ 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.
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'); }
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.
instanceof
operator to check if a value is a numberYou should never use the instanceof
operator to check if a value is of type
number.
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 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.
const myVar = 5; if (typeof myVar === 'number') { // ๐๏ธ this runs console.log('โ value is a number'); } else { console.log('โ๏ธ value is NOT a number'); }
You can learn more about the related topics by checking out the following tutorials: