How to check if a value is a number in JavaScript
New Courses Coming Soon
Join the waiting lists
How is it possible to determine if a variable value is a number?
We have various ways to check if a value is a number.
The first is isNaN()
, a global variable, assigned to the window
object in the browser:
const value = 2
isNaN(value) //false
isNaN('test') //true
isNaN({}) //true
isNaN(1.2) //false
If isNaN()
returns false, the value is a number.
Another way is to use the typeof
operator. It returns the 'number'
string if you use it on a number value:
typeof 1 //'number'
const value = 2
typeof value //'number'
So you can do a conditional check like this:
const value = 2
if (typeof value === 'number') {
//it's a number
}
→ Get my JavaScript Beginner's Handbook
→ Read my
JavaScript Tutorials
on The Valley of Code
→ Read my
TypeScript Tutorial
on The Valley of Code
Here is how can I help you:
- COURSES where I teach everything I know
- CODING BOOTCAMP cohort course - next edition in 2025
- THE VALLEY OF CODE your web development manual
- BOOKS 17 coding ebooks you can download for free on JS Python C PHP and lots more
- Interesting links collection
- Follow me on X