forloop 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: 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

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

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

Example 4: javascript for loop

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

Example 5: for loop javascript

var listItem = [
        {name: 'myName1', gender: 'male'},
        {name: 'myName2', gender: 'female'},
        {name: 'myName3', gender: 'male'},
        {name: 'myName4', gender: 'female'},
      ]

    for (const iterator of listItem) {
        console.log(iterator.name+ ' and '+ iterator.gender);
    }

Example 6: forloop in js

for(i = 0; i < x; ++i){
  //Loop, x can be replaced with any integer; 
}

Tags:

Cpp Example