DEV Community

Cover image for Understanding Looping in JavaScript
Annapoorani Kadhiravan
Annapoorani Kadhiravan

Posted on

Understanding Looping in JavaScript

Programming often requires performing the same task multiple times. Writing the same code repeatedly is inefficient and difficult to maintain. This is where loops come into the picture.

Loops allow us to execute a block of code repeatedly until a specified condition becomes false.


Why Do We Need Loops?

Suppose we want to print 1 five times.

Without loops, we would need to write:

let count = 0;

if (count < 5) {
    console.log(1);
    count++;
}

// Repeat the same block 4 more times
Enter fullscreen mode Exit fullscreen mode

Repeating the same block several times increases the number of lines and makes the program difficult to manage.

To avoid this repetition, JavaScript provides loops.


The while Loop

A while loop is a shorter and more efficient way to execute the same block of code multiple times.

Syntax

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

The loop continues executing as long as the condition evaluates to true.


Example 1: Print 1 Five Times

let count = 0;

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

Output

1
1
1
1
1
Enter fullscreen mode Exit fullscreen mode

How It Works

  1. count starts at 0.
  2. The condition count < 5 is checked.
  3. Since the condition is true, 1 is printed.
  4. count++ increases the value by 1.
  5. The process repeats until count becomes 5.
  6. At that point, the condition becomes false and the loop stops.

Infinite Loop

Consider the following code:

let count = 0;

while (count < 5) {
    console.log(1);
}
Enter fullscreen mode Exit fullscreen mode

Since count is never incremented, the condition always remains true.

As a result, the loop keeps running forever, creating an infinite loop.

Therefore, always ensure that the loop variable is updated properly.


Example 2: Print Numbers from 1 to 5

let i = 1;

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

Output

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Initial value = 1
  • Ending value = 5
  • Interval = +1

The variable increases by one after each iteration.


Example 3: Print Numbers from 5 to 1

let i = 5;

while (i >= 1) {
    console.log(i);
    i--;
}
Enter fullscreen mode Exit fullscreen mode

Output

5
4
3
2
1
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Initial value = 5
  • Ending value = 1
  • Interval = -1

The variable decreases after each iteration.


Example 4: Find the Sum of the First Five Numbers

let i = 1;
let sum = 0;

while (i <= 5) {
    sum += i;
    i++;
}

console.log(sum);
Enter fullscreen mode Exit fullscreen mode

Output

15
Enter fullscreen mode Exit fullscreen mode

Step-by-Step

Iteration Value of i Sum
1 1 1
2 2 3
3 3 6
4 4 10
5 5 15

Example 5: Print Odd Numbers

Odd numbers can be generated by increasing the value by 2 in each iteration.

let i = 1;

while (i <= 9) {
    console.log(i);
    i += 2;
}
Enter fullscreen mode Exit fullscreen mode

Output

1
3
5
7
9
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Initial value = 1
  • Ending value = 9
  • Interval = 2

Components of a Loop

Every loop generally contains three important parts:

1. Initial Value

let i = 1;
Enter fullscreen mode Exit fullscreen mode

Determines where the loop starts.


2. Condition

i <= 5
Enter fullscreen mode Exit fullscreen mode

Determines when the loop should continue executing.


3. Increment or Decrement

i++;
Enter fullscreen mode Exit fullscreen mode

or

i--;
Enter fullscreen mode Exit fullscreen mode

Changes the loop variable after each iteration.


Short Summary

  • Loops help avoid writing repetitive code.
  • A while loop executes repeatedly as long as its condition is true.
  • Every loop requires:

    • Initial value
    • Condition
    • Increment or decrement
  • Forgetting to update the loop variable results in an infinite loop.

  • Loops can be used to print numbers, calculate sums, and generate patterns efficiently.


References
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while?utm_source=chatgpt.com
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration?utm_source=chatgpt.com
https://www.w3schools.com/js/js_loop_while.asp?utm_source=chatgpt.com
https://javascript.info/while-for?utm_source=chatgpt.com

Top comments (0)