for i in range javascript code example

Example 1: js loop

var colors=["red","blue","green"];
for (let i = 0; i < colors.length; i++) { 
  console.log(colors[i]);
}

Example 2: javascript loop

let array = ['Item 1', 'Item 2', 'Item 3'];

array.forEach(item => {
	console.log(item); // Logs each 'Item #'
});

Example 3: javascript loop through array

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

Example 4: range javascript

[...Array(5).keys()];
 => [0, 1, 2, 3, 4]

Example 5: 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 6: javascript loop through array

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

Tags:

Java Example