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 array = ['Item 1', 'Item 2', 'Item 3'];
2
3array.forEach(item => {
4 console.log(item); // Logs each 'Item #'
5});
1let array = ['Item 1', 'Item 2', 'Item 3'];
2
3// Here's 4 different ways
4for (let index = 0; index < array.length; index++) {
5 console.log(array[index]);
6}
7
8for (let index in array) {
9 console.log(array[index]);
10}
11
12for (let value of array) {
13 console.log(value); // Will each value in array
14}
15
16array.forEach((value, index) => {
17 console.log(index); // Will log each index
18 console.log(value); // Will log each value
19});
1/*Loops are great tools when you need your program to run a code block a
2certain number of times or until a condition is met, but they need a
3terminal condition that ends the looping. Infinite loops are likely to
4freeze or crash the browser, and cause general program execution mayhem,
5which no one wants.
6
7Infinite loop example(do not call this function!):*/
8function loopy() {
9 while(true) {
10 console.log("Hello, world!");
11 }
12}
13
14/*Loop example with terminal condition:*/
15function myFunc() {
16 for (let i = 1; i <= 4; i += 2) {
17 console.log("Still going!");
18 }
19}