- Check if value is number javascript
- # Check if a Value is a Number in JavaScript
- # Check if a Value is a Number using the typeof operator
- # Check if a Value is a Number using Number.isFinite()
- # Check if a Value is a Number using NaN
- # Check if a Variable is a Number using lodash
- # Don’t use the instanceof operator to check if a value is a number
- # Additional Resources
- Check that variable is a number
Check if value is number javascript
Last updated: Jan 6, 2023
Reading time · 5 min
# Check if a Value is a Number in JavaScript
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.
# 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.
Copied!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.
Copied!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.
Copied!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 flips it.
The Number.isNaN method checks if the passed-in value is NaN (not a number) AND its type is number .
Copied!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 .
# 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 .
Copied!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:
Copied!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.
Copied!// ⛔️ 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 .
Copied!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:
Copied!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.
Copied!# 👇️ 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.
Copied!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.
# 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.
Copied!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.
Copied!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.
Check that variable is a number
The javascript function isNaN(variable) should do the trick. It returns true if the data is not a number.
This is a good suggestion. FWIW, this works because strings are implicitly converted to numbers — the explicit code is: isNaN(new Number(variable)) . Note that if you wish to restrict the code to integers, parseInt() will return NaN (testable via isNaN() ). Be aware though that parseInt() will happily ignore non-numberic digits at the end of the string.
this will accept everything which can be converted to a number, including boolean values and ‘infinity’ ; better use isFinite(String(foo))
Don’t use isNaN, it’s broken. See developer.mozilla.org/en-US/docs/JavaScript/Reference/… and use the solution proposed by @Christoph instead.
See this answer for an explanation why. If you only want to accept integer values, look here.
isFinite(«100») is true, we want to know that an object is a Number object. From my brief testing it looks like the solution here (Object.prototype.toString.call(obj) === «[object Number]») is the best: stackoverflow.com/questions/1303646/…
Does not handle all variables: isFinite(String(» «)) == isFinite(String(«\t»)) == isFinite(String(«»)) == true . See the answer linked.
I’m pretty new to javascript but it seems like typeof(blah) lets you check if something is a number (it doesn’t say true for strings). A know the OP asked for strings + numbers but I thought this might be worth documenting for other people.
function isNumeric(something)
and here’s a few console runs of what typeof produces:
typeof(12); "number" typeof(null); "object" typeof('12'); "string" typeof(12.3225); "number"
one slight weirdness I am aware of is
but it wouldn’t be javascript without something like that right?!
It’s better JS style to use typeof(something) == ‘number’ . With == rather than === . Reserve === for when you really mean it.
@AdamChalcraft most linters/programmers would probably disagree and say the opposite — always use === unless there’s a good reason not to, if you’ll get into the habit of using == you’ll get burned by something bizaare happening at some point. In this case there’s no type coercion going on so it’s a fairly moot point either way. For an educational (and also hilarious) rant on the topic check out James Mickens’ «To wash it all away» scholar.harvard.edu/files/mickens/files/towashitallaway.pdf