Loops By Chloe
For Loops
For loops include three expressions that implement the repeated execution of a code block.for(let i=0; i<4; i++){ console.log(i); } => 0,1, 2, 34 would not be included because 4 is not less than 4 so does not meet the criteria.
Loops and Arrays
Loops can be used to modify arrays, coming in handy when you don't know the full length of an array or want to add elements into an array more efficiently.let example =[]; for(let i=0; i < 3; i++){ example.push(i); console.log(example); }; => [0] [0, 1] [0, 1, 2]When you don't know the length of the array but want to iterate through it, the .length method can be used. The loop will stop when the number of loops equals the length of the array.
let bike =['evil', 'giant', 'norco', 'conmencal']; for (let i=0; i < bike.length; i++){ console.log(bike[i]); }; = > 'evil' 'giant' 'norco' 'conmencal'
Using the For...Of loop for Arrays
The for...of method is a newer method in the ECMA-6 language. It is a way of looping through an array, similar to above but using less code, this make the chances of error less and makes it more read-able.let bikes =['evil', 'giant', 'norco', 'conmencal']; for (let bike of bikes){ console.log(bike); }; => 'evil' 'giant' 'norco' 'conmencal'As you can see in the example, you get the same results as before, but the code is cleaner.
let bikes =['evil', 'giant', 'norco', 'conmencal']; for (let [index, bike] of bikes.entries()) { console.log(index, bike); }; => 0 'evil' 1 'giant' 2 'norco' 3 'conmencal'
Loops and Objects Using the For...In method
The for...in method iterates over properties of an object, this can be useful to easily access the properties of an object instead of looking through all the code which can take time. To show how this is done, a simple object of a bike will be used, using the for...in method to access all the property values, property names and then linking them together.var bike ={ brand: "conmencal", model: "furious", year: 2019, style: "downhill" }; for(attribute in bike){ console.log(attribute); }; => brand model year styleTo access the property value you use the property name as the index value of the object:
for(attribute in bike){ console.log(bike[attribute]); }; => conmencal furious 2019 downhillTo link them all together you would do the following:
for(attribute in bike){ console.log(`${attribute}` + : `${bike[attribute]}`); }; => brand: conmencal model: furious year: 2019 style: downhill
While Loops
The while loop is the most basic loop to construct in Javascript. The while loop will loop through multiple times as long as the specified condition is true. The syntax is as follows:let i=10 while (i<10){ console.log(“I am happy!”); i++; } => “I am happy!” “I am happy!” “I am happy!” “I am happy!” “I am happy!” “I am happy!” “I am happy!” “I am happy!” “I am happy!” “I am happy!”
Do...While loops
Do...while loops are similar to while loops, however, it will always execute the code at least once, even if the condition is not true. The syntax for the do...while loop is:let x = 10; do { i++ console.log(i) } while(i>9){ console.log(i + 'is less than 9'); } => 11
Infinite loops, break and continue keyword.
Infinite loops are loops that continue forever because the condition is always true.let i = 0; for (i=0; i<100; i++){ console.log(i); if (i===50) break; } the loop will break after 50.The continue keyword will skip an iteration and then continue on with the loop as normal:
var i; for (i=0; i<101; i++){ if ((i%2)===0){ continue; } console.log(i); } This will only log odd numbers.