Operating on a string or array when we expect a number can bring strange results in our code. In this article, we'll look at various functions that can help determine if a variable we are using is a number.
-Infinity are numbers too - though, we'll be ignoring those values.
With these requirements, the best function to use is the
isFinite() function from the built-in
However, developers have commonly used other functions for this purpose, particularly
Number.isNaN() and the
Let's create a few variables for testing:
let intVar = 2; let floatVar = 10.5; let stringVar = '4'; let nanVar = NaN; let infinityVar = Infinity; let nullVar = null; let undefinedVar = undefined;
Using the Number.isFinite() function
Number.isFinite() function checks if the variable is a number, but also checks if it's a finite value. Therefore, it returns
false on numbers that are
Let's test it out on the variables we've defined above:
> Number.isFinite(intVar); true > Number.isFinite(floatVar); true > Number.isFinite(stringVar); false > Number.isFinite(nanVar); false > Number.isFinite(infinityVar); false > Number.isFinite(nullVar); false > Number.isFinite(undefined); false
It's exactly what we wanted. The special non-finite numbers are ignored, as well as any variable that isn't a number type.
If you'd like to check if a variable is a number, your best bet is to use the
Using the Number.isNaN() Function
Number object has an
isNaN() method. It takes one argument, and determines if its value is
NaN. Since we want to check if a variable is a number, we will use the not operator,
!, in our checks.
Now let's check if the not operator and
Number.isNaN() function can filter only numbers:
> !Number.isNaN(intVar); true > !Number.isNaN(floatVar); true > !Number.isNaN(stringVar); true # Wrong > !Number.isNaN(nanVar); false > !Number.isNaN(infinityVar); true # Wrong > !Number.isNaN(nullVar); true # Wrong > !Number.isNaN(undefinedVar); true # Wrong
This method is a fairly permissive as it accepts values that are not numbers at all. This method is best suited for when you know you have a number and would to check if it is a
NaN value, not for general number checking.
Using the typeof() function
typeof()shows as object)
function(a special type of object)
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
To verify if a variable is a number, we simply we need to check if the value returned by
"number". Let's try it out on test variables:
> typeof(intVar) == 'number'; true > typeof(floatVar) == 'number'; true > typeof(stringVar) == 'number'; false > typeof(nanVar) == 'number'; true # Wrong > typeof(infinityVar) == 'number'; true # Wrong > typeof(nullVar) == 'number'; false > typeof(undefined) == 'number'; false
typeof() function performs much better than
Number.isNaN(). It correctly determines that a string variable,
undefined are not numbers. However, it returns true for
While that is the technically correct result,
Infinity are special number values, for most use cases we would prefer to ignore them.
Number.isNaN() function is only suitable if we know our variable is a number and need to verify if it specifically
NaN or otherwise.
typeof() function is suitable if your code can work with
-Infinity as well as other numbers.
Number.isFinite() method captures all the finite numbers and was most appropriate for our requirements.