The JavaScript Array.every() method is used to test whether all elements in an array pass a given condition (provided as a callback function). If every element satisfies the condition, it returns true; otherwise, it returns false. This method stops executing the callback function as soon as it finds an element that does not meet the condition, making it efficient for large arrays.
Unlike loops or forEach(), the every() method does not modify the original array and is often used for validation purposes.
Syntax
The syntax for the Array.every() method is given below:
array.every(callback(element, index, array), thisArg)
Parameters
The Array.every() method accepts two parameters:
- callback (Required): The callback function that is executed on each element. It takes three arguments:
- element: The current element being processed in the array.
- index (Optional): The index of the current element.
- array (Optional): The array that is being processed.
thisArg (Optional): Value to use as this when executing the callback function. If not provided, undefined is used.
Return Value
The method returns true if all elements in the array pass the test implemented by the callback function. If one or more elements fail the test, it returns false.
Examples of JavaScript Array.every() Method
Example 1: Checking if All Elements Are Greater Than a Value
You can use the every() method to check if all elements in an array meet a certain condition.
const numbers = [10, 20, 30, 40];
const isGreaterThanFive = numbers.every(num => num > 5);
console.log(isGreaterThanFive); // Output: true
Explanation: In this example, the every() method checks whether all elements in the numbers array are greater than 5. Since every number is greater than 5, the result is true.
Example 2: Checking if All Elements Are Even
You can use the every() method to determine if all numbers in an array are even.
const numbers = [2, 4, 6, 8];
const areAllEven = numbers.every(num => num % 2 === 0);
console.log(areAllEven); // Output: true
Explanation: This example checks if all elements are even numbers by using the condition num % 2 === 0. Since all elements are even, the method returns true.
Example 3: Early Termination of the every() Method
The every() method stops evaluating as soon as it encounters a value that fails the condition.
const numbers = [5, 12, 8, 130, 44];
const isAllAbove10 = numbers.every(num => num > 10);
console.log(isAllAbove10); // Output: false
Explanation: The method stops execution as soon as it encounters the element 5, which is not greater than 10. Since not all elements satisfy the condition, the method returns false.
Example 4: Using thisArg with the every() Method
You can provide a thisArg argument to be used as the this context inside the callback function.
const ages = [18, 24, 35, 42];
const ageLimit = {
minAge: 18
};
const isEligible = ages.every(function(age) {
return age >= this.minAge;
}, ageLimit);
console.log(isEligible); // Output: true
Explanation: Here, we provide thisArg (the ageLimit object) to the every() method. Inside the callback, the this context is set to ageLimit, and the method checks if all ages are greater than or equal to minAge.
Example 5: Checking an Empty Array
If the every() method is called on an empty array, it will always return true.
const emptyArray = [];
const result = emptyArray.every(num => num > 10);
console.log(result); // Output: true
Explanation: The every() method returns true for an empty array since there are no elements to test against the condition.