The Wayback Machine - https://web.archive.org/web/20220924080709/https://www.geeksforgeeks.org/javascript-array-every-method/amp/

JavaScript Array every() Method

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. 




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

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: 

function ispositive(element, index, array) {
  return element > 0;
}
print([11, 89, 23, 7, 98].every(ispositive)); 

Output: 

true
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: 




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




<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().




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: 




Article Tags :