for loop 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: for loop javascript
var i;
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
Example 3: javascript for loop
for (var i; i < 10; i++) {
}
Example 4: js for loop
var i; //defines i
for (i = 0; i < 5; i++) { //starts loop
console.log("The Number Is: " + i); //What ever you want
}; //ends loop
//Or:
console.log("The Number Is: " + 0);
console.log("The Number Is: " + 1);
console.log("The Number Is: " + 2);
console.log("The Number Is: " + 3);
console.log("The Number Is: " + 4);
//They do the same thing!
//Hope I helped!
Example 5: javascript for
let str = "";
for (let i = 0; i < 9; i++) {
str = str + i;
}
console.log(str);
// expected output: "012345678"
Example 6: for loop js
let arr = [];
for(let i = 0; i <= 10; i++){
arr[i] = Math.round(Math.random() * 10);
}
• Result: [3, 1, 1, 2, 3, 9, 5, 6, 6, 8, 5]
• Or
let arr = [];
for(let i = 0; i <= 10; i++){
arr[i] = Math.trunc(Math.random() * 10);
}
Result[7, 6, 0, 2, 4, 8, 7, 5, 5, 6, 5]