Check If Array Contains Only Numbers In JavaScript

Don’t worry if you need to check if array contains only numbers. This article will share some solutions to check if array contains only numbers in JavaScript using the isNaN() method. Let’s get started.

How to check if array contains only numbers in JavaScript

We will use the isNaN() method to check if array contains only numbers. First, we will learn about the isNaN() method, its syntax, and its usage.

The isNaN() function returns true if a value is NaN when converted to a number.

Syntax:

isNaN(value)

Parameters:

value: the value to be tested.

Example:

// Input is a number
console.log(isNaN(4));

// Input is a string of characters
console.log(isNaN("4"));
console.log(isNaN("LearnShareIT"));

// Input is an empty string
console.log(isNaN(""));

Output:

false
false
true
false

The isNaN() method will return false for falsy values.

To check if the array contains only numbers, we will use the some() and isNaN() methods. If the some() method returns true, it means that at least one element of the array is NaN when converted to a number.

Example:

// Check if array contains only numbers
function checkNum(arr) {
    // Check if array contains falsy values (other than 0)
    if (arr.some((value) => value !== 0 && !value === true)) {
        return false;
    } else {
        return !arr.some(isNaN);
    }
}

// Create an array of strings
const strArr = ["Learn", "Share", "IT"];
console.log(checkNum(strArr));

// Create an array of falsy values
const falsyArr = ["", undefined, null];
console.log(checkNum(falsyArr));

// Create an array of numbers
const numArr = [0, "1", 2, "3"];
console.log(checkNum(numArr));

Output:

false
false
true

We used the NOT operator (!) to change the function’s output to true if none of the array’s elements are NaN when converted to numbers (the array contains only numbers).

Another solution to check if array contains only numbers is using the isNaN() and every() methods. We have shared the syntax and usage of every() method in another post. Click here to learn more.

Example:

// Check if array contains only numbers
function checkNum(arr) {
    // Check if array contains falsy values (other than 0)
    if (arr.some((value) => value !== 0 && !value === true)) {
        return false;
    } else {
        return !arr.every(isNaN);
    }
}

// Create an array of strings
const strArr = ["Learn", "Share", "IT"];
console.log(checkNum(strArr));

// Create an array of falsy values
const falsyArr = ["", undefined, null];
console.log(checkNum(falsyArr));

// Create an array of numbers
const numArr = [0, "1", 2, "3"];
console.log(checkNum(numArr));

Output:

false
false
true

Summary

This article shared two solutions to check if array contains only numbers in JavaScript using the isNaN() method. You can use this method in combination with the some() or every() methods to keep your code looking clean. Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *