Javascript Reference - How to loop through array and work on each element in Javascript








every() accepts two arguments.

  • a function to run on each item
  • an optional scope object.

every() runs the given function on every item and returns true if the function returns true for every item. every() does the query against the array for matching criteria with the passed in function.

The function passed in receives three arguments.

  • the array item value,
  • the position of the item in the array
  • the array object itself.




Example


var numbers = [1,2,3,4,5,4,3,2,1]; 
        
var everyResult = numbers.every(function(item, index, array){ 
   return (item > 2); 
}); 
        
console.log(everyResult); //false 

The code above generates the following result.

every() does not change the values contained in the array.