How to check if a value is number in JavaScript

Aug 14, 2023#javascript

JavaScript is a dynamic and loosely typed language, which means that variables can hold different types of values and change their type at runtime. This can sometimes lead to confusion or errors when performing operations that expect a certain type of value, such as arithmetic or comparison.

In JavaScript, several data types can be considered as numbers:

  • Integer numbers (42, 1000)
  • Floating-point numbers (3.14, 0.111)
  • Scientific notation numbers (1e5, 2e10)
  • Numeric strings ("3.14", "42")

TLDR;

Use either Number.isNaN() or isNaN() in combination with the typeof operator. This function will return true only if the value is of type "number" and not equal to NaN:

function isNumber(value) {
  return typeof value === "number" && !Number.isNaN(value);
}

// Test some values
console.log(isNumber(42));      // true
console.log(isNumber(3.14));    // true
console.log(isNumber(1e5));     // true
console.log(isNumber("42"));    // false
console.log(isNumber("3.14"));  // false
console.log(isNumber("abc"));   // false
console.log(isNumber(""));      // false
console.log(isNumber(NaN));     // false
console.log(isNumber(true));    // false
console.log(isNumber(['a']));   // false

Here’s another solution if you’re also interested in numeric string:

function isNumber(value) {
  return !isNaN(parseFloat(value)) && isFinite(value);
}

// Test some values
console.log(isNumber(42));      // true
console.log(isNumber(3.14));    // true
console.log(isNumber(1e5));     // true
console.log(isNumber("42"));    // true
console.log(isNumber("3.14"));  // true
console.log(isNumber("abc"));   // false
console.log(isNumber(""));      // false
console.log(isNumber(NaN));     // false
console.log(isNumber(true));    // false
console.log(isNumber(['a']));   // false

Using typeof operator

One way to check if a value is a number in JavaScript is to use the typeof operator. This operator returns a string that indicates the type of the value.

console.log(typeof 42);       // "number"
console.log(typeof "hello");  // "string"
console.log(typeof true);     // "boolean"
console.log(typeof [1, 2]);   // "object"
console.log(typeof NaN);      // "number"

You can use this operator to compare the type of a value with the string "number" and see if it matches. For example:

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

console.log(isNumber(42));   // true
console.log(isNumber("42")); // false
console.log(isNumber(true)); // false
console.log(isNumber(null)); // false
console.log(isNumber(NaN));  // true ❌

However, there is one caveat with this method. The typeof operator will also return "number" for the special value NaN, which stands for Not a Number. This value is the result of an invalid or undefined mathematical operation, such as dividing by zero or parsing a non-numeric string.

This can be misleading, since NaN is not a valid number and cannot be used in calculations. To avoid this problem, you can use another method to check if a value is NaN before checking its type.

Using isNaN function

Another way to check if a value is a number in JavaScript is to use the isNaN() function. This function returns true if the value is NaN, and false otherwise. However, there are two versions of this function in JavaScript: Number.isNaN() and isNaN(). They have different behaviors and use cases.

The Number.isNaN() function is part of the Number object and only checks if the value is of type number and equal to NaN. It does not perform any type coercion or conversion on the value.

console.log(Number.isNaN(42));       // false
console.log(Number.isNaN("42"));     // false
console.log(Number.isNaN(NaN));      // true
console.log(Number.isNaN("hello"));  // false

The isNaN() function is part of the global object and first tries to convert the value to a number, then checks if it is equal to NaN. It performs type coercion on the value, which can sometimes lead to unexpected results.

console.log(isNaN(42));      // false
console.log(isNaN("42"));    // false
console.log(isNaN(NaN));     // true
console.log(isNaN("hello")); // true ❌

Notice that the last example returns true, even though "hello" is not a number. This is because the isNaN function tries to parse "hello" as a number, which results in NaN, and then checks if it is equal to NaN, which returns true. This can be confusing and cause errors in your code.

Using parseInt or parseFloat functions

Keep in mind that parseFloat() and parseInt() might not be the best choices for all scenarios, especially if you need to handle complex numeric formats or edge cases. But for most typical use cases, they provide a simple and effective way to check if a value is a number.

function isNumber(value) {
  return !isNaN(parseFloat(value)) && isFinite(value);
}

console.log(isNumber(42));      // true
console.log(isNumber(42));      // true
console.log(isNumber(3.14));    // true
console.log(isNumber(1e5));     // true
console.log(isNumber("42"));    // true
console.log(isNumber("3.14"));  // true
console.log(isNumber("abc"));   // false
console.log(isNumber(""));      // false
console.log(isNumber(NaN));     // false
console.log(isNumber(true));    // false
console.log(isNumber(['a']));   // false

In this example, the isNumber function checks if a value is a number by attempting to parse it using parseFloat(). It also checks if the parsed value is not NaN (using isNaN()) and if it’s finite (using isFinite()). This approach works for both integers and floating-point numbers, as well as strings that can be successfully converted to numbers.

Using regular expressions

Using a combination of typeof and isNaN() is generally simpler and more straightforward for basic numeric checks. It’s a good choice for most common cases where you want to determine if a value is a number or not.

The regular expression approach can become complex and harder to manage as you try to cover more edge cases and numeric formats, especially when dealing with scientific notation, localized number formats, and other variations.

This regular expression will match both integers and floating-point numbers, including negative numbers and numeric strings.

function isNumber(value) {
    return /^-?\d+(\.\d+)?$/.test(value);
}

console.log(isNumber(42));       // true
console.log(isNumber("3.14"));   // true
console.log(isNumber("-42"));    // true
console.log(isNumber("hello"));  // false
console.log(isNumber("42abc"));  // false
console.log(isNumber("42."));    // false

For accurate numeric validation, especially in cases where precision and edge cases matter, it’s often recommended to use built-in parsing functions.