looping over an array code example

Example 1: iterate through array javascript

const array = ["one", "two", "three"]
array.forEach(function (item, index) {
  console.log(item, index);
});

Example 2: iterate through an array

var arr = [1,2,3,4,5,6,7,8];

// Uses the usual "for" loop to iterate
for(var i= 0, l = arr.length; i< l; i++){
	console.log(arr[i]);
}

console.log("========================");

//Uses forEach to iterate
arr.forEach(function(item,index){
	console.log(item);
});

Example 3: for loop in java as long as array

for(i = 0; i < arrayName.length; i++{
  	//print statement will output current index's value
 	System.out.println(arrayName[i]); 
}

Tags:

Java Example