DEV Community

Rakshambika
Rakshambika

Posted on

Loop in JS

What is Loop?

  • A loop is used to execute a block of code repeatedly as long as a specified condition is true. The loop stops when the condition becomes false.
  • A loop is used to perform the same task multiple times without writing the same code repeatedly.

Without Loop:

console.log("Welcome");
console.log("Welcome");
console.log("Welcome");
console.log("Welcome");
console.log("Welcome");
Enter fullscreen mode Exit fullscreen mode

With Loop:

let i=1;
while(i<=5)
{
console.log("Welcome");
i++;
}

Enter fullscreen mode Exit fullscreen mode
Welcome
Welcome
Welcome
Welcome
Welcome

Enter fullscreen mode Exit fullscreen mode

Why do we use loop?

  • Reduce the lines of code.
  • Without loops, we need to write the same code multiple times manually. Using loops, we can execute a block of code repeatedly and automatically until the specified condition becomes false.
  • Easier to maintain.
  • Used to iterate arrays, objects...

Types of Loop:

There are two types of Loops:

Entry Controlled Loops:

  • Any loop where we check the test condition before entering the loop is an entry controlled loop.
  • If the condition is false initially, the loop body will not execute even once.

Exit Controlled Loops:

  • Any loop where we check the test condition after the statements are executed once is an exit controlled loop.
  • The loop body executes at least once, even if the condition is initially false.
             While loop
             do...while loop
             for loop
             for .. in loop
             for .. of loop
Enter fullscreen mode Exit fullscreen mode

While loop:

  • While loop is an entry-controlled loop.
  • It executes the block of code when the condition is true, when the condition becomes false, the loop stops.

Syntax:

while(condition)
{
//code
}
Enter fullscreen mode Exit fullscreen mode

Example:

// While loop

let n=1;

while(n<=5)
{
    console.log(n);
    n++;
}
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

Here:

  1. Check 1 <= 5 → true → print 1
  2. Check 2 <= 5 → true → print 2
  3. Check 3 <= 5 → true → print 3
  4. Check 4 <= 5 → true → print 4
  5. Check 5 <= 5 → true → print 5
  6. Check 6 <= 5 → false → stop the loop

do.. While Loop:

  • Do... While is a exit-controlled loop.
  • The loop body executes at least once, even if the condition is initially false.
  • The condition is checked after executing the loop body.

Syntax:

do
{

//code

}while(condition)
Enter fullscreen mode Exit fullscreen mode

Example:

//do while

let i = 10;

do {
    console.log(i);
    i++;
} while (i <= 5);

Enter fullscreen mode Exit fullscreen mode

Output:

10
Enter fullscreen mode Exit fullscreen mode

Here, i <= 5 is initially false, but 10 is printed because the loop body executes before the condition is checked.


For Loop:

  • For loop is an entry-controlled loop.
  • For loop is used when the number of iteration is known.
  • In for loop, initialization, condition, and increment/decrement are written in the same line, separated by semicolons (;).

Syntax:

for (initialization; condition; increment/decrement) {
    // code to execute
}
Enter fullscreen mode Exit fullscreen mode

Three Parts of for Loop:

for (let i = 1; i <= 5; i++)
     ↑          ↑       ↑
     |          |       |
Initialization Condition Update
Enter fullscreen mode Exit fullscreen mode

Initialization → Executes only once at the beginning.
Condition → Checked before every iteration.
Increment/Decrement → Updates the value after each iteration.

Example:

//for loop
for (let i = 1; i <= 5; i++) {
    console.log(i);
}
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

for .. in Loop:

  • The for...in loop is used to iterate over the keys (property names) of an object.
  • In each iteration, the variable contains the key/index, not the value.

Syntax:

for (let key in object) {
    // code
}
Enter fullscreen mode Exit fullscreen mode

Example:

// for in
let Data={
   name:"Raksha",
   location:"Thiruvarur",
   isStudent:true,
   age:21
}

for(let key in Data)
{
    console.log(key);
}
Enter fullscreen mode Exit fullscreen mode

Output:

name
location
isStudent
age
Enter fullscreen mode Exit fullscreen mode

Example:

let animals=["Lion","Tiger","Cat"]


for(let index in animals)
{
    console.log(index);
}
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
2
Enter fullscreen mode Exit fullscreen mode

for .. of Loop:

  • The for...of loop is used to iterate over the values of an iterable objects.
  • It is commonly used with arrays, strings, Sets, and Maps.
  • It directly gives you the value, not the key/index.

Syntax:

for (let value of iterable) {
    // code
}
Enter fullscreen mode Exit fullscreen mode

Example:

let fruits = ["Apple", "Banana", "Mango"];

for (let fruit of fruits) {
    console.log(fruit);
}
Enter fullscreen mode Exit fullscreen mode

Output:

Apple
Banana
Mango
Enter fullscreen mode Exit fullscreen mode

Top comments (0)