Skip to main content

How to Check if a Value is a Number in JavaScript

Validating data types is a fundamental task in JavaScript. A common requirement is to check if a variable holds a number before performing a calculation or other operation. While this seems simple, JavaScript's type system has some quirks (like NaN) that require a careful approach.

This guide will teach you the modern and most reliable methods for checking if a value is a number. We will cover the typeof operator and the Number.isFinite() method, explaining the pros and cons of each and why the global isNaN() function should be avoided.

The Core Problem: What is a "Number"?

When we check if a value is a number, we usually want to exclude certain "numeric" but invalid values, such as:

  • NaN (Not a Number), which surprisingly has a typeof 'number'.
  • Infinity and -Infinity.
  • Strings that look like numbers (e.g., '123').

The best method depends on how strictly you want to define "number."

Method 1: The typeof Operator

The typeof operator is the most direct way to check a variable's primitive type. It returns the string "number" for any numeric value, including NaN and Infinity.

For example, you need a basic check to see if a variable is of the number type.

const value = 123;

Solution:

function isNumber(value) {
return typeof value === 'number';
}

console.log(isNumber(123)); // Output: true
console.log(isNumber('123')); // Output: false
console.log(isNumber(NaN)); // Output: true (This is a notable edge case!)
console.log(isNumber(Infinity)); // Output: true (Another edge case)

While simple, this method is often not strict enough because it allows NaN and Infinity. To make it more robust, you would have to add an extra check:

function isActualNumber(value) {
return typeof value === 'number' && !Number.isNaN(value) && Number.isFinite(value);
}

This is becoming verbose, which is why there's a better method.

The Number.isFinite() method is the modern and recommended best practice for a strict number check. It returns true only if the provided value is a number that is not Infinity, -Infinity, or NaN.

This single method handles all the edge cases for you.

const value1 = 123;
const value2 = '123';
const value3 = NaN;
const value4 = Infinity;

// Number.isFinite() is the most reliable check.
console.log(Number.isFinite(value1)); // Output: true
console.log(Number.isFinite(value2)); // Output: false
console.log(Number.isFinite(value3)); // Output: false
console.log(Number.isFinite(value4)); // Output: false
note

This is the cleanest and most accurate way to confirm that a value is a "real," usable number.

The Common Pitfall: isNaN() and Type Coercion

You should avoid using the global isNaN() function to check if a value is a number. This function has confusing behavior because it first tries to coerce (convert) the value to a number before checking it.

The Problem with isNaN():

// These results are confusing and often not what you want.
console.log(isNaN('123')); // Output: false (because '123' can be converted to a number)
console.log(isNaN(true)); // Output: false (because `true` becomes `1`)
console.log(isNaN(null)); // Output: false (because `null` becomes `0`)
console.log(isNaN('hello')); // Output: true
note

Because of this aggressive type coercion, !isNaN(value) is an unreliable way to check if value is a number. The modern Number.isNaN() is much safer because it does not perform type coercion.

Conclusion

For checking if a value is a number in JavaScript, the method you choose depends on your required level of strictness.

  • The typeof value === 'number' check is a quick and simple way to check the primitive type, but remember that it will return true for NaN and Infinity.
  • The Number.isFinite(value) method is the recommended best practice. It is the most robust and reliable way to check if a value is a "real" number, as it correctly returns false for non-numbers, NaN, and Infinity.
  • Avoid the global isNaN() function, as its type coercion behavior leads to confusing and unreliable results.