array iterate for loop js code example

Example 1: loop array javascript

var colors = ['red', 'green', 'blue'];
	
	colors.forEach((color, colorIndex) => {
     console.log(colorIndex + ". " + color); 
    });

Example 2: loop an array in javascript

let array = ["loop", "this", "array"]; // input array variable
for (let i = 0; i < array.length; i++) { // iteration over input
	console.log(array[i]); // logs the elements from the current input
}

Example 3: how to use for loops to work with array in javascript

for(let i = 0; i < myArray.length; i++){

Example 4: javascript foreach call specific value in array

run.addEventListener("click", function () {
    people.forEach((element) => console.log(element.firstname));
  }); // select a specific string in array

Tags:

Java Example