JavaScript Array every: Determining If All Array Elements Pass a Test

Summary: in this tutorial, you will learn how to check whether all the array elements pass a test using the JavaScript Array every() method.

Checking array elements using the for loop

Sometimes, you need to test whether every element of an array satisfies a specified condition.

Typically, you use a  for loop to iterate all elements and check each individual element against the condition. Suppose that you have an array numbers with three elements:

let numbers = [1, 3, 5];Code language: JavaScript (javascript)

The following code checks if every element in the numbers array is greater than zero:

let numbers = [1, 3, 5];
let result = true;
for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] <= 0) {
        result = false;
        break;
    }
}
console.log(result);Code language: JavaScript (javascript)

Output:

trueCode language: JavaScript (javascript)

How it works:

  • First, initialize the  result variable to true.
  • Second, iterate over the elements of the numbers array and check whether each element is less than or equal zero. If it is the case, set the result variable to false and terminate the loop immediately using the break statement. In case no element is less than or equal zero, the value of the result variable remains  true.

This code is simple and straight forward. However, it is quite verbose.

JavaScript Array type provides the every() method that allows you to check if every element of an array pass a test in a shorter and cleaner way.

Introduction to JavaScript Array every() method

Starting from ES5, JavaScript Array type provides a method every() that tests every element in an array.

The following example uses the every() to check if every element of the numbers array is greater than zero:

let numbers = [1, 3, 5];
let result = numbers.every(function (e) {
    return e > 0;
});

console.log(result);Code language: JavaScript (javascript)

Output:

trueCode language: JavaScript (javascript)

By using the ES6 arrow functions, the code can be even shorter:

let numbers = [1, 3, 5];

let result = numbers.every( e  => e > 0);

console.log(result);Code language: JavaScript (javascript)

It is also much cleaner, isn’t it?

The following illustrates the syntax of the every() method.

arrayObject.every(callback[, thisArg])Code language: CSS (css)

The every() method accepts two named arguments: callback and thisArg.

1) The callback argument

The callback is a function that tests each element of the array. The callback() function has the following form:

function callback(currentElement, index, array){
   //...
}Code language: JavaScript (javascript)

The callback() function takes three arguments:

  • First, the currentElement is the current element that is being processed.
  • Second, the index is the index  of the currentElement.
  • Third, the array is the array that the every() method was called upon.

The currentElement argument is required whereas the index and array arguments are optional.

2) The thisArg argument

The thisArg argument of the every() method is optional. If you pass the thisArg argument into the method, the this value inside the callback function will reference the thisArg argument.

The every() method returns true if the callback function returns a truthy value for every array element; otherwise, it returns false.

Note that the every() method executes the callback() function on every element in the array until it finds the one that causes the callback() return a falsy value.

In other words, the every() will stop calling the callback() function and return false once there is an array element that causes callback() to return a falsy value.

Let’s take a look at some more examples of using the every() method.

More JavaScript Array every() method examples

The following example tests whether all the array elements are the even numbers

let numbers = [1, 3, 5];
let isEven = numbers.every(function (e) {
    return e % 2 == 0;
});

console.log(isEven);Code language: JavaScript (javascript)

Output:

falseCode language: JavaScript (javascript)

In contrast, the following example tests if all the array elements are the odd numbers.

let numbers = [1, 3, 5];

let isOdd = numbers.every(function (e) {
    return Math.abs(e % 2) == 1;
});

console.log(isOdd);Code language: JavaScript (javascript)

Output:

trueCode language: JavaScript (javascript)

Suppose that you have an object with two properties: min and max:

let range = {
    min: 0,
    mas: 10
};Code language: JavaScript (javascript)

The following example tests whether all elements in the numbers array is in the range specified by the min and max of the range object.

let numbers = [1, 3, 5];

let range = {
    min: 0,
    max: 10
};

let isInRange = numbers.every(function (e) {
    return e >= this.min && e <= this.max;
}, range);Code language: JavaScript (javascript)

Output:

trueCode language: JavaScript (javascript)

In this example, we pass the range object to the every() method as the second argument. And inside the callback() function, we reference the range object using the this keyword.

Caution: Empty arrays

If you call the every() method on an empty array, the method will always return true for any condition. For example:

let gtZero = [].every(e => e > 0); // any condition
let ltZero = [].every(e => e < 0); // any condition

console.log('gtZero:', gtZero);
console.log('ltZero:', ltZero);Code language: JavaScript (javascript)

Output:

gtZero: true
ltZero: trueCode language: JavaScript (javascript)

In this tutorial, you have learned how to use the JavaScript Array every() method to test whether all elements in an array pass the test provided by a test function.

Was this tutorial helpful ?