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

Description

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]; 
        /*from  w  w w  .j a va2  s. c om*/
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.





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window