iterating over an array javascript code example

Example 1: javascript iterate array

var txt = "";
var numbers = [45, 4, 9, 16, 25];

numbers.forEach(function(value, index, array) {
  txt = txt + value + "<br>";
});

Example 2: iterate array javascript

array = [ 1, 2, 3, 4, 5, 6 ]; 
for (index = 0; index < array.length; index++) { 
    console.log(array[index]); 
}

Example 3: javascript foreach index

users.forEach((user, index)=>{
	console.log(index); // Prints the index at which the loop is currently at
});

Example 4: javascript iterate array

String[] myStringArray = {"Hello", "World"};
for (String s : myStringArray)
{
    // Do something
}

Tags:

Java Example