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!
1//for ... in statement
2
3const object = { a: 1, b: 2, c: 3 };
4
5for (const property in object) {
6 console.log(`${property}: ${object[property]}`);
7}
8
9// expected output:
10// "a: 1"
11// "b: 2"
12// "c: 3"
13
1let array = ['Item 1', 'Item 2', 'Item 3'];
2
3array.forEach(item => {
4 console.log(item); // Logs each 'Item #'
5});
1let array = ["foo", "bar"]
2
3let low = 0; // the index to start at
4let high = array.length; // can also be a number
5
6/* high can be a direct access too
7 the first part will be executed when the loop starts
8 for the first time
9 the second part ("i < high") is the condition
10 for it to loop over again.
11 the third part will be executen every time the code
12 block in the loop is closed.
13*/
14for(let i = low; i < high; i++) {
15 // the variable i is the index, which is
16 // the amount of times the loop has looped already
17 console.log(i);
18 console.log(array[i]);
19} // i will be incremented when this is hit.
20
21// output:
22/*
23 0
24 foo
25 1
26 bar
27*/