Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript Array every() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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: This method accepts five parameters as mentioned above and described below:

  • 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.

Let us see an example below which is of the Array every() method which is here used to check whether the array elements are even or not. 

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




// JavaScript code for every() method
function isEven(element, index, array) {
    return element % 2 == 0;
}
function func() {
    let arr = [56, 92, 18, 88, 12];
 
    // Check for even number
    let value = arr.every(isEven);
    console.log(value);
}
func();


Output: 

true

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




// JavaScript code for every() method
function ispositive(element, index, array) {
    return element > 0;
}
 
function func() {
    let arr = [11, 89, 23, 7, 98];
 
    // Check for positive number
    let value = arr.every(ispositive);
    console.log(value);
}
func();


Output: 

true

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




// JavaScript code for every() method
function isodd(element, index, array) {
    return element % 2 == 1;
}
 
function func() {
    let arr = [56, 91, 18, 88, 12];
 
    // Check for odd number
    let value = arr.every(isodd);
    console.log(value);
}
func();


Output: 

false

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: The browsers supported by JavaScript Array every() method are listed below: 

  • 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.


My Personal Notes arrow_drop_up
Last Updated : 18 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials