JavaScript

Iterations

Iterations can gives you a quick and easy way to somehting repeatly. It is also called loops
There are:

For loops

for loops repeat a block of code as long as a certain condition is met.

for ([initialExpression]; [conditionExpression]; [incrementExpression]){ 
statement
};

initialExpression is the counter variable
conditionExpression is evaluated in the beginning of each iteration until it is false.
incrementExpression updates the loop counter

Examples on for loops

for (let step = 0; step < 5; step++) { 
// Runs 5 times, with values of step 0 through 4.
console.log(`Walking ${step} step`);
};
var food = ["apple", "pancakes", "orange"];
for (let i = 0; i < food.length; i++) {
// Runs as many times as the length of the array
console.log(food[i]);
};

While loops

While loops is simple. It goes though a block of code as long as the condition is true.

while (condition){ 
statement
};

Examples on while loops

let i = 0;
while (i < 10) {
// code to be repeated
console.log('Rep ' + i);
i++;
};
console.log('after loop ' + i);
let arr = [];
let count = 0;
const size = 5;

while(count < size) {
count++;
arr.push(count);
console.log('The current size of the array is ' + count);
};
console.log(arr);

Do while loops

The do while loops is almost like the while loop. The main difference is that first the statement is executed and then expression is checked.

do{ 
statement
} while (condition){
statement
};

Examples on while loops

let x = 0;
do {
x++;
console.log(x);
} while (false);

The do..while loop will loops first and is followed by the while (condition), which will only run, if it is true.
To test it there are here a do..while loop, where we have writen false inside the condition.

let i = 1;
do{
console.log("The number is " + i);
i++;
}
while(i <= 5);

There are made a variable let i = 1; and a do..while loop. Inside the do is there a console.log("The number is " + i); which prints out what the variable value is. The i++; is an incrementExpresssion. The while(i <= 5); will check if the condition is true. If it is false will it not work anymore.

For...in

The for...in statement iterates through the properties of an object. The block of code inside the loop will be executed once for each property. For...in cannot be uses in arrays or strings. But it can be used with objects.

for (var in object) {
code block to be executed
};

Examples on while loops

let object = { a: 1, b: 2, c: 3 };

for (const prop in object) {
console.log(`${prop}: ${object[prop]}`);
};

A object is made, and a for... in statement is made. There are made a variable name called prop. Then the iteration is executed will the prop values be the objects properties a, b, c and the object property value 1,2,3 be visible with the object variable object and the prop variable.

For...of

The for...of statement creates a loop iterating over iterable objects.

for (var of object) {
code block to be executed
};

Examples on while loops

const array1 = ['a', 'b', 'c'];

for (const element of array1) {
console.log(element);
};

The for...of statement gives the array index a new name, called element. Then the variable name elements is called will the index value be printet out.

const iterable = 'boo';

for (const value of iterable) {
console.log(value);
};