Skip to main content

.forEach()

.forEach( callback ( n Element, index Int, arr array) ): self

Iterates throught the elements matched, executes a callback function for each iteration.

Difference between .each() vs .forEach()
.forEach() acts as the regular Array.forEach(), where the arguments order is the callback is item, index, array
In .each() similarly to jQuery the index argument comes first: index, item, array
// No arguments
$( "p" ).forEach(function() {
console.log( $( this ).text() );
});

// With arguments
$( "p" ).forEach(function( el, index, arr ) {
console.log( 'Paragraph ' + index + ": " + $( this ).text() );
});