loop function js code example

Example 1: javascript for loop

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

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: for loop javascript

var i;
for (i = 0; i < 10 ; i++) {
  //do something
}

Example 4: for next loop javasxcrop

for (i = 0; i < 5; i++) {}

Example 5: loop javascript

for (var i = 0; i < cats.length; i++) {
  if (i === cats.length - 1) {
    info += 'and ' + cats[i] + '.';
  } else {
    info += cats[i] + ', ';
  }
}

Example 6: javascript loop

var items=["a","b","c"];
for(let item of items){
  console.log(item)
}

Tags:

Java Example