javascript each function code example

Example 1: jquery each

$( "li" ).each(function( index ) {
  console.log( index + ": " + $( this ).text() );
});

Example 2: jquery loop over elements

$( "li" ).each(function( index ) {
  console.log( index + ": " + $( this ).text() );
});

Example 3: javascript foreach

const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
	console.log(index, item)
})

Example 4: jquery loop through li elements

//looping through list elements in jquery
$('#myUlID li').each(function() {
  console.log($(this));
})

Example 5: javascript foreach

var colors = ['red', 'blue', 'green'];

colors.forEach(function(color) {
  console.log(color);
});

Example 6: for each js

const fruits = ['mango', 'papaya', 'pineapple', 'apple'];

// Iterate over fruits below

// Normal way
fruits.forEach(function(fruit){
  console.log('I want to eat a ' + fruit)
});

Tags:

Dart Example