JavaScript Loops

What are JavaScript Loops?

JavaScript loops are used to repeat a block of code until a specified condition is met. This allows for the repetition of actions in a program, saving time and reducing code duplication.

Types of JavaScript Loops

  1. For Loop: Executes a block of code a specified number of times.
  2. For…of Loop: Iterates over values of iterable objects like arrays or strings.
  3. For…in Loop: Iterates over the enumerable properties of an object.
  4. While Loop: Executes a block of code as long as a specified condition is true.

Array Methods for Looping

JavaScript also provides methods for looping through arrays:

  • Array.forEach: Executes a function once for each array element.
  • Array.values(): Returns an iterator for all values in an array.
  • Array.keys(): Returns an iterator for all keys in an array.
  • Array.map(): Creates a new array with results of calling a function on every element.
  • Array.reduce(): Reduces the array to a single value based on a specified function.

Definition of Loops

Loops allow for the repeated execution of a block of code. They are used to iterate over instructions multiple times until a certain condition is met. In JavaScript, the for loop is commonly used to iterate over arrays and perform a task for each element.

Example of a For Loop


let myArray = [1, 2, 3, 4, 5];
for (let i = 0; i < myArray.length; i++) {
    console.log(myArray[i]);
}

In this example, the loop iterates over the elements of myArray, logging each element to the console. The loop consists of three parts: initialization (let i = 0), condition (i < myArray.length), and increment (i++).

Importance of Loops in Programming

Loops are crucial for repeating actions, controlling program flow, and optimizing repetitive tasks. They allow us to iterate through data structures such as arrays and objects efficiently, saving time and reducing potential errors.

Types of Loops in JavaScript

The For Loop

A for loop allows repeated execution of a block of code. It consists of initialization, condition, and increment.


for (let i = 0; i < 5; i++) {
    console.log(i);
}

This loop prints numbers 0 to 4. The loop continues as long as the condition i < 5 is true, incrementing i by 1 after each iteration.

The While Loop

A while loop continues to run as long as a specified condition is true.


let count = 0;
while (count < 5) {
    console.log(count);
    count++;
}

This loop prints numbers 0 to 4. It stops when count is no longer less than 5.

The Do...While Loop

A do...while loop runs at least once before checking the condition.


let count = 0;
do {
    console.log(count);
    count++;
} while (count < 5);

This loop also prints numbers 0 to 4. It guarantees at least one execution before evaluating the condition.

Preventing Infinite Loops

To avoid infinite loops, ensure that the condition in the loop will eventually become false. This involves updating the loop variable or using a break statement to exit the loop when necessary.

Example of Avoiding Infinite Loops


let count = 0;
while (count < 5) {
    console.log(count);
    count++;
}

Here, count is incremented in each iteration, ensuring the loop will eventually stop.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate