What is the every() Method in JavaScript?

Introduction

The every() method in JavaScript is used for arrays. It checks whether all elements in the array fulfill a specific condition provided as a callback function. If all elements meet the condition, it returns true. If any element does not meet the condition, even if the rest do, it returns false. The every() method ensures that every element in the array meets the specified condition, it will only return true if this is the case.

How Does It Work?

The every() method is similar to a loop, such as a for loop or forEach loop, that iterates over the elements of an array. It starts from the first element and continues to the last element, checking each one to see if it meets a specified condition. If any element does not meet the condition, every() returns false. If all elements meet the condition, it returns true.

It accepts three arguments: element, index, and array. The first argument, element, is used for accessing each element within the array. The second argument, index, provides the index or position of each element. The third argument, array, represents the entire array and is rarely used, which is why it is optional. You can see examples and details about each argument below:

element

Description: The current element in the array.
Usage: You use this to evaluate the test condition.
Example: In array.every(element => element < 7), element represents each item in the array, and the test checks if it is less than 7.

index

Description: The index of the current element in the array.
Usage: Useful when the test condition depends on the position of the element in the array. Example: In array.every((element, index) => index % 2 === 0), index is used to determine if the position of the element is even.

arr

Description: The array on which every() was called.
Usage: Allows access to the entire array from within the callback function, which can be helpful for comparisons or checks involving the entire array.
Example: In array.every((element, index, arr) => element < arr[index + 1]), arr allows you to compare each element with the next one in the array.

Note: You can use any names for the parameters instead of element, index, and array. These are just parameter names, and you can choose any names you prefer.

every() Method in Practical:

Example 1

JavaScript

copy

 let array = [1, 2, 3, 4, 5, 5];
 console.log(array.every(key => key > 1));
 // output: false

In this example, the every() method is used to check whether all elements in the array are greater than 1 (key > 1). The output is false because not all elements meet the condition; specifically, element 1 does not satisfy key > 1.

Example 2

JavaScript

copy

 let array = [1, 2, 3, 4, 5, 7];
 console.log(array.every(key => key < 7));
 // output: false

In Example 2, the every() method is used with the condition key < 7 to check if all elements in the array are less than 7. The output is false because element 7 does not meet the condition, even though all other elements do.

Example 3

JavaScript

copy

 let array = [1, 2, 3, 4, 5, 7];
 console.log(array.every(key => key < 8));
 // output: true

In Example 3, the every() method is used with the condition key < 8. The output is true because all elements in the array are less than 8, so the condition is satisfied for every element.