How to Check if Elements of Array Satisfy a Given Condition (Javascript) Use-Cases of this Tutorial

• Find whether elements of an array are satisfying a given condition.
• Know about Array.some() method
• Know about Array.every() method

Quite often it is required to know whether elements of an array satisfy a given condition. A simple example could be checking if all elements of array are greater than 100, or at least one element is greater than 100.

Instead of using a loop to perform these checks we can use native Javascript methods to do this which are better optimized. Furthermore they make code look "beautiful".

Concepts and Methods Used

• Array.every() : This array method checks whether all elements of an array pass the condition given through a given function.
• Array.some() : This array method checks whether at least one elements of the array pass the condition given through the given function.

Checking whether All Array Elements Satisfy the Condition

The every() method in Javascript tests all the elements of a given array against a condition and returns a boolean true on success. If any one element does not pass the test, false is returned.

The condition to check is passed as a callback function to the every() method. This callback function is called for each element in the array — the element and index are passed as its parameters. If the element satisfies the condition in the callback we need to return true, and false otherwise.

We demonstrate the working of every() with a simple example — we will check if all elements of array are less than 10.

// input array
var arr = [1, 2, 3, 4, 5];

//	1
//	2
//	3
//	4
//	5
//	true
arr.every(function(element, index) {
console.log(element);

// check whether element passes condition
// if passed return true, if fails return false
if(element < 10)
return true;
else
return false;
});

Note that if the callback returns false, the iteration over the array stops and the method exits. The next elements will not be checked.

var arr = [1, 2, 3, 4, 5];

//	1
//	false
arr.every(function(element, index) {
console.log(element);

if(element > 5)
return true;
else
return false;
});

Checking whether At Least One Array Element Satisfy the Condition

The some() method checks whether at least one element of the calling array satisfy the condition. On finding the first element satisfying the condition it returns true. If it failed to find any element that satisfies the condition it returns false.

Just like every(), the condition to check is provided by a callback function. The element and its index are passed as the parameters to this callback function.

As an example, suppose we need to check whether an given array contains a value greater than 3 :

var arr = [1, 2, 3, 4, 5];

//	1
//	2
//	3
//	4
//	true
arr.some(function(element, index) {
console.log(element);

if(element > 3)
return true;
else
return false;
});

Note that some() stops the iteration of the array and exits as soon as the condition is satisfied.

Useful Resources Adding Elements to an Array with Javascript Reversing an Array with Javascript Removing Elements from an Array with Javascript