Loop and iteration
Loop offer a quick and easy way to do something repeatedly, you can think of a loop as a computerized version of the game where you tell someone to take X steps in one direction then Y steps in another. There are many ways to do loop in js let's see all this methods.
You will see that we have many ways to declare loops in javascript but all works exactly the same in the background:
- The first step in a loop is the initialization we declare or we use a variable generally
i
it's a kind of convention, and we give her a value. - The second is the condition, this will return a boolean if the result is true we can continue through the loop steps if it's false then we stop the process.
- The last step is the final expression this will be evaluated at the end of each loop iteration.
for loop
The syntax for loop is really useful when you want to cross an element whose size you know.
for (let i = 0; i < 10; i++) {
console.log(i);
}
- Let's see this basic for loop in details.
- We initialize our variable with a value of
0
. - We now check if the condition is true and it is because
i
=0
and0
is inferior to10
. - The condition is true we can execute the code inside our loop.
- The final step is the last statement we can now increment our variable
i++
so from nowi
is equal to1
. - The process is done we can repeat all this steps until the condition is false.
While loop
The while loop syntax is useful when you have an element with a size that you ignore.
let i = 0;
while( i < 10) {
console.log(i);
i++;
}
- The while loop works the same than for loop but with a different syntax.
- You have to initialize your variable outside the while condition.
- Use the
while
keyword and write your condition. - Write your code inside the while
{}
. - Don't forget to add at the end of the while statement the last step
i++
or you will have an infinite loop and it will block your web browser.
ForEach loop
The forEach loop is a little bit different because she is available only when it's an collection of a DOM element.
DomElement.forEach( el => {
console.log(el);
} );
- Once you've got a collection of a DOM element you have access to the
forEach
method who works like a loop. - You have to declare one parameter who can be equal to the
i
variable seen above.
Continue statement
The continue instruction allow us to skip the execution of the loop at a certain moment.
let noms = ["lulu","bubu","tutu",23,"riri"];
for (let i = 0; i < noms.length; i++) {
if ( typeof noms[i] !== String) {
continue;
} else {
console.log(noms[i]);
}
};
- The javascript loop add a couple of instructions like the
continue
keywords. - In our example we use the
continue
keywords to skip the element which is different from aString
data type. - The result in the console will be
lulu bubu tutu riri
.
Break statement
The continue instruction allow us to stop the execution of the loop at a certain moment.
let noms = ["lulu","bubu","tutu",23,"riri"];
for ( let i = noms.length -1; i >= 0; i-- ) {
if ( typeof noms[i] !== String ) {
break;
} else {
console.log(noms[i]);
}
}
- The
break
keyword is the opposite of thecontinue
keyword. - The
break
keyword will stop the execution of the loop when an element of thenoms
array will be different from aString
data type.
Current errors
This is a list of the main errors that you can meet when you use loop:
Note: I'm not a wizard there is maybe some issue that you notice above so fell free to open an issue in the github repo if you find a new error not mentioned above.