The JavaScript Array.every() method considers all the elements of an array and then further checks whether all the elements of the array satisfy the given condition (passed by in user) or not that is provided by a method passed to it as the argument.
Syntax:
// Arrow function
every((element) => { /* … */ })
every((element, index) => { /* … */ })
every((element, index, array) => { /* … */ })
// Callback function
every(callbackFn)
every(callbackFn, thisArg)
// Inline callback function
every(function (element) { /* … */ })
every(function (element, index) { /* … */ })
every(function (element, index, array) { /* … */ })
every(function (element, index, array) { /* … */ }, thisArg)
Parameters:
- callback: This parameter holds the function to be called for each element of the array.
- element: The parameter holds the value of the elements being processed currently.
- index: This parameter is optional, it holds the index of the current value element in the array starting from 0.
- array: This parameter is optional, it holds the complete array on which Array.every is called.
- thisArg: This parameter is optional, it holds the context to be passed as this is to be used while executing the callback function. If the context is passed, it will be used like this for each invocation of the callback function, otherwise undefined is used as default.
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.
Example 1: In this example, 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();
|
Example 2: In this example 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();
|
Example 3: In this example, the method every() checks if every number in the array is odd or not. Since some of the numbers are even, therefore this method returns false.
Javascript
function isodd(element, index, array) {
return element % 2 == 1;
}
function func() {
let arr = [56, 91, 18, 88, 12];
let value = arr.every(isodd);
console.log(value);
}
func();
|
Example 4: In this example, 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
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browsers:
- Google Chrome 1 and above
- Microsoft Edge 9.0 and above
- Mozilla Firefox 1.5 and above
- Safari 3 and above
- Opera 9.5 and above
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
29 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...