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 returnstrue. If any element does not meet the condition, even if the rest do, it returnsfalse. Theevery() 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() returnsfalse. If all elements meet the condition, it returnstrue.

It accepts three arguments:element,index, andarray. 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:Inarray.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:Inarray.every((element, index) => index % 2 === 0), index is used to determine if the position of the element is even.

arr

Description: The array on whichevery() 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:Inarray.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, andarray. 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, theevery() method is used to check whether all elements in the array are greater than1 (key > 1). The output is false because not all elements meet the condition; specifically, element1 does not satisfykey > 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, theevery() method is used with the condition key < 7 to check if all elements in the array are less than7. The output isfalse because element7 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, theevery() 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.

Final Thoughts on every()

The every() method is a built-in JavaScript method for arrays. It checks whether all the elements in the array meet a specific condition. If all elements satisfy the condition, it returns true; otherwise, it returns false. The method accepts three arguments: element, index, and array. Here, element refers to each individual element in the array, index refers to the position of each element, and array refers to the entire array.