for loop javascript 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'];
for (let index = 0; index < array.length; index++) {
console.log("index", index);
console.log("array item", array[index]);
}
Example 3: javascript for loops
JavaScript For Loop: Summary
There are three types of for loops: the regular for loop, the for/in loop and for/of loop.
The for loop iterates through an array.
The for/in loop iterates through the properties of an object.
The for/of loop iterates through iterable objects, like arrays and strings.
Example 4: for of loop javascript
const array1 = ['a', 'b', 'c'];
for (const element of array1) {
console.log(element);
}
Example 5: how to make a javascript for loop
var tops = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144];
for (let i = 0; i < tops.length; i++) {
document.write(tops[i] + "tops");
}
Example 6: for loop in javascript
For printing 0 to 9
you can write like below also
var i;
for (i = 0; i < 10; i++) {
console.log(i);
}