JavaScript Array every() Method
Last Updated :
05 Mar, 2024
JavaScript Array every() method checks if all elements in an array pass a test specified by a function. It returns true if all elements satisfy the condition, otherwise false. Useful for validation or checking conditions on array elements.
Array every() Method Syntax
array.every(callback(element, index, array), thisArg);
Array every() Method Parameters
callback
: A function to test each element of the array. It takes three arguments:
element
: The current element being processed in the array.
index
(optional): The index of the current element being processed.
array
(optional): The array every()
was called upon.
thisArg
(optional): An object to use this
when executing the callback function.
Array every() Method Return value
This method returns a Boolean value true if all the elements of the array follow the condition implemented by the argument method. If any one of the elements of the array does not satisfy the argument method, then this method returns false.
Array every() Method Examples
Example 1: Checking If all elements are even or not
Here, the method every() checks if a number is even for every element of the array. Since the array does not contain odd elements therefore this method returns true as the answer.
Javascript
function isEven(element, index, array) {
return element % 2 == 0;
}
function func() {
let arr = [56, 92, 18, 88, 12];
let value = arr.every(isEven);
console.log(value);
}
func();
|
Explanation
- In this example we Applies the every() method to array arr using the isEven callback function.
- Checks if every element in arr satisfies the condition of being even.
- Returns true if all elements are even; otherwise, returns false.
Example 2: Checking if all elements are positive
Here, the method every() checks if a number is positive for every element of the array. Since the array does not contain negative elements therefore this method returns true as the answer.
Javascript
function ispositive(element, index, array) {
return element > 0;
}
function func() {
let arr = [11, 89, 23, 7, 98];
let value = arr.every(ispositive);
console.log(value);
}
func();
|
Explanation
- In this example we use the every() method to check if all elements in array arr are positive.
- Defines a callback function isPositive returning true if an element is greater than 0, else false.
- Returns true as all elements are positive, satisfying the condition.
Example 3: Checking If one array is exactly the subset of another array
Here, we will check whether one array is exactly the subset of another array or not using several methods like every() as well as includes().
Javascript
let check_subset = (first_array, second_array) => {
return second_array.every((element) => first_array.includes(element));
};
console.log(
"Subset Condition Satisfies? : " + check_subset([1, 2, 3, 4], [1, 2])
);
console.log(
"Subset Condition Satisfies? : " + check_subset([1, 2, 3, 4], [5, 6, 7])
);
|
Output
Subset Condition Satisfies? : true
Subset Condition Satisfies? : false
Explanation
- In this example we Defines a function check_subset to determine if second_array is a subset of first_array.
- Utilizes the every method to iterate through second_array and checks if each element is included in first_array using the includes method.
- Returns true if all elements in second_array are found in first_array, indicating that second_array is a subset
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browsers
Share your thoughts in the comments
Please Login to comment...