It's a way to loop through particular list of data in order to get the result at the end. Loops will run till the condition turns out to be false. We have for, while and do while.
I have curated 5 loops that you can find almost every single day.
i = 1;
<, >, ≤ , ≥
Syntax:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
// INSTEAD OF WRITING SAME CODE 10 TIMES
console.log(REP -1)
console.log(REP -2)
console.log(REP -3)
console.log(REP -4)
console.log(REP -5)
console.log(REP -6)
console.log(REP -7)
console.log(REP -8)
console.log(REP -9)
console.log(REP -10)
// WE CAN DO SOMETHING LIKE THIS 👇🏻
for (let rep = 1; rep <= 10; rep++) {
console.log(`REP - ${rep}`);
}
Loop inside a loop 🔁 :
for (let excercise = 1; excercise < 4; excercise++) {
console.log(` --- Starting Exercise - ${excercise}`);
for (let rep = 1; rep <= 5; rep++) {
console.log(` Excercise ${excercise} REP - ${rep} 🏋🏼♀️`);
}
}
The JavaScript for in
statement loops through the properties of an Object and arrays.
Do not use for in over an Array if the index order is important. It is better to use a for loop, a for of loop, when the order is important.
Syntax:
for (key in object) {
// code block to be executed
}