for loop example javascript
Example 1: javascript for loop
var colors=["red","blue","green"];
for (let i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
Example 2: for loop javascript
var i;
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
Example 3: javascript loop
let array = ['Item 1', 'Item 2', 'Item 3'];
for (let index = 0; index < array.length; index++) {
console.log(array[index]);
}
for (let index in array) {
console.log(array[index]);
}
for (let value of array) {
console.log(value);
}
array.forEach((value, index) => {
console.log(index);
console.log(value);
});
Example 4: for in loop javascript
let array = [1,2,3,4,5,6,7,8,9,10];
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
Example 5: javascript for loop[
for(i=0;i<=5;i++){
console.log(i);}
Example 6: how to do a for loop in js
for (let i = 0; i < 10; i++) {
console.log(i);
}