Loops in Javascript.

A loop is a programming tool that is used to repeat a set of instructions. Iterate is a generic term that means “to repeat” in the context of loops. A loop will continue to iterate until a specified condition, commonly known as a stopping condition, is met.

For Loop

for loop declares looping instructions, with three important pieces of information separated by semicolons ;:

  • The initialization defines where to begin the loop by declaring (or referencing) the iterator variable.
  • The stopping condition determines when to stop looping (when the expression evaluates to false).
  • The iteration statement updates the iterator each time the loop is completed.
for (let i = 0; i < 4; i += 1) {
  console.log(i);
}

The output would be:

0
1
2
3

While Loop

The while loop creates a loop that is executed as long as a specified condition evaluates to true. The loop will continue to run until the condition evaluates to false. The condition is specified before the loop, and usually, some variable is incremented or altered in the while loop body to determine when the loop should stop.

while (condition) {
  // Code block to be executed
}

For example:

let i = 0;

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

The output would be:

0
1
2
3
4

Do…While Loop

dowhile statement creates a loop that executes a block of code once, checks if a condition is true, and then repeats the loop as long as the condition remains true. They are used when the loop body needs to be executed at least once. The loop ends when the condition evaluates to false.

let x = 0;
let i = 0;

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

The output would be:

0
1
3
6
10

Reverse Loop

for loop can iterate “in reverse” by initializing the loop variable to the starting value, testing for when the variable hits the ending value, and decrementing (subtracting from) the loop variable at each iteration.

const items = ['apricot', 'banana', 'cherry'];

for (let i = items.length - 1; i >= 0; i -= 1) {
  console.log(`${i}. ${items[i]}`);
}

The output would be:

2. cherry
1. banana
0. apricot

Related Posts

Top String Functions / Methods in JavaScript

Regular Expression in JavaScript for password validation with special character and number and string.

Password Validation in JavaScript / React

Minimum eight characters, at least one letter, one number and one special character: let ppattern = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/ Regular Expression in JavaScript for password validation with special character…

What are custom events in JavaScript?

Custom events are the events that allow you to decouple the code you want to run after a specific piece of code runs. There are various in-built events…

How to use nested for loop in JavaScript?

We use the for loop statement of JavaScript for repeating a set of statements inside the loop body a specified number of times. A nested for loop, as the…

What are the basic rules for JavaScript parameters?

A JavaScript function is a code that performs a particular task. The function parameters are the name list in the function definition. Parameters are also known as…

How to stop refreshing the page on submit in JavaScript?

Using event.preventDefault() to stop page refresh on form submit In this section, we will see how to use event.preventDefault() to stop page refresh on form submission. The event.preventDefault() restricts the default…