Skip to content
Related Articles

Related Articles

JavaScript Array every() Method
  • Last Updated : 02 Feb, 2021

Below is the example of Array every() method to check the array elements even or not. 

  • Example:

Javascript




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


Output: 

true

The arr.every() method checks whether all the elements of the array satisfy the given condition or not that is provided by a method passed to it as the argument. 

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

Supported Browsers: The browsers supported by JavaScript Array every() method are listed below: 

  • Google Chrome
  • Microsoft Edge 9.0
  • Mozilla Firefox 1.5
  • Safari
  • Opera

 

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :