Skip to content
Related Articles

Related Articles

JavaScript Array every() Method

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 10 Jun, 2022

This article covers all the facts in detail which are related to the method under Array provided by JavaScript which is Array.every() method.

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

  • Example:

Javascript




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

Output: 

true

The Array.every() method considers all the elements of an array  and then further checks that 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 argu

ment. 

Syntax:  

arr.every(callback(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 currentValue 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 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 Boolean value true if all the elements of the array follow the condition implemented by the argument method. If one of the elements of the array does not satisfy the argument method, then this method returns false.

Below Examples illustrate the method in JavaScript: 

  • Example 1: 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.
function ispositive(element, index, array) {
  return element > 0;
}
print([11, 89, 23, 7, 98].every(ispositive)); 

Output: 

true
  • Example 2: 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.
function isodd(element, index, array) {
  return (element % 2 == 1);
}
print([56, 91, 18, 88, 12].every(isodd)); 

Output: 

false

Codes for above function are as follows:

Program 1: 

Javascript




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

Output: 

true

Program 2: 

Javascript




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

Output: 

false

Example-3: 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

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!