1var colors=["red","blue","green"];
2for (let i = 0; i < colors.length; i++) {
3 console.log(colors[i]);
4}
1var i; //defines i
2for (i = 0; i < 5; i++) { //starts loop
3 console.log("The Number Is: " + i); //What ever you want
4}; //ends loop
5//Or:
6console.log("The Number Is: " + 0);
7console.log("The Number Is: " + 1);
8console.log("The Number Is: " + 2);
9console.log("The Number Is: " + 3);
10console.log("The Number Is: " + 4);
11//They do the same thing!
12//Hope I helped!
1let str = '';
2
3for (let i = 0; i < 9; i++) {
4 str = str + i;
5}
6
7console.log(str);
8// expected output: "012345678"
9
1/*
2The for loop is very useful in JavaScript
3syntax:
4for (initialiser; condition; increment) {
5 code
6}
7*/
8// print each number between 0 and 9
9for (let i = 0; i < 10; i++) {
10 console.log(i);
11}
12
13// for loops are useful when working with arrays
14let array = ["a", "b", "c"];
15// print each element in the array
16for (let i = 0; i < array.length; i++) {
17 console.log(array[i]);
18}
19
20/*
21Often for loops start at 0 rather than 1
22because they are so commonly used with arrays.
23*/