Check if Array contains only Numbers in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
3 min

banner

# Check if an Array contains only Numbers in JavaScript

To check if an array contains only numbers:

  1. Use the Array.every() method to iterate over the array.
  2. Check if the type of each element is number.
  3. The every() method will return true if the array contains only numbers and false otherwise.
index.js
const arr1 = [1, 2, 3]; const arr2 = [1, 2, 'three']; const arr3 = ['1', '2', '3']; function onlyNumbers(array) { return array.every(element => { return typeof element === 'number'; }); } console.log(onlyNumbers(arr1)); // ๐Ÿ‘‰๏ธ true console.log(onlyNumbers(arr2)); // ๐Ÿ‘‰๏ธ false console.log(onlyNumbers(arr3)); // ๐Ÿ‘‰๏ธ false

check if array contains only numbers

The code for this article is available on GitHub

The function we passed to the Array.every() method gets called with each element in the array until it returns a falsy value or iterates over the entire array.

If the function returns a falsy value, the every() method short-circuits returning false.

On each iteration, we check if the element has a type of number and return the result.

The condition has to be satisfied for all array elements for the every() method to return true.

If your array contains numbers that might be of type string, use the following approach instead.

index.js
const arr1 = [1, 2, 3]; const arr2 = [1, 2, 'three']; const arr3 = ['1', '2', '3']; function onlyNumbers(array) { return array.every(element => { return !isNaN(element); }); } console.log(onlyNumbers(arr1)); // ๐Ÿ‘‰๏ธ true console.log(onlyNumbers(arr2)); // ๐Ÿ‘‰๏ธ false console.log(onlyNumbers(arr3)); // ๐Ÿ‘‰๏ธ true

We used the isNaN() function to determine if the passed-in value can be converted to a number.

index.js
console.log(isNaN('test')); // ๐Ÿ‘‰๏ธ true console.log(isNaN('1')); // ๐Ÿ‘‰๏ธ false

An easy way to think about it is:

  1. The isNaN() (is Not a Number) function tries to convert the passed-in element to a number and returns true if it can't.
  2. We negate the result by using the logical NOT ! operator, to check if all elements in the array can be converted to numbers.

You can also use a for...of loop to check if an array contains only numbers.

# Check if an array contains only numbers using a for...of loop

This is a three-step process:

  1. Use a for...of loop to iterate over the array.
  2. Check if the type of each element is not equal to number.
  3. If the condition is met, return false and exit the loop.
index.js
function onlyNumbers(array) { let containsOnlyNumbers = true; for (const element of array) { if (typeof element !== 'number') { containsOnlyNumbers = false; break; } } return containsOnlyNumbers; } const arr1 = [1, 2, 3]; const arr2 = [1, 2, 'three']; const arr3 = ['1', '2', '3']; console.log(onlyNumbers(arr1)); // ๐Ÿ‘‰๏ธ true console.log(onlyNumbers(arr2)); // ๐Ÿ‘‰๏ธ false console.log(onlyNumbers(arr3)); // ๐Ÿ‘‰๏ธ false

check if array contains only numbers using for of loop

The code for this article is available on GitHub

The for...of statement is used to loop over iterable objects like arrays, strings, Map, Set and NodeList objects and generators.

On each iteration, we check if the current element doesn't have a type of number.

If the condition is met, we set the containsOnlyNumbers boolean to false and use the break statement to exit the for...of loop.

The onlyNumbers() function takes an array and returns true if the array contains only numbers and false otherwise.

# 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.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev