TechMentorAI

loop

in JavaScript

Published

Loops in JavaScript are a way of repeating a block of code a number of times until a certain condition is met. This is helpful when you want to run the same code repeatedly, each time with a different value.

Imagine loop in JavaScript

Types of Loops

In JavaScript, there are several types of loops: 'for' loops, 'while' loops, and 'do while' loops. 'for' loops are the most common and are typically used when you know exactly how many times you want to loop through a block of code. 'while' loops continue until a specified condition evaluates to false. 'do while' loops will always execute the body at least once, and then it will repeat the loop as long as the condition is true.

For loop

A 'for' loop in JavaScript is made up of initialization, condition and increment/decrement. Initialization is where we set our counter to a starting point. The condition is encountered to determine if the loop should keep going or stop. The increment or decrement adjusts the value of the counter.

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

While loop

In a 'while' loop, if the condition is true, the code within the block is executed. This will continue until the condition is no longer met.

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

Do While loop

A 'do while' loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

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

No Time to Read? Learn on the Go!

By reading this article, you've invested 1.21 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.

A loop is like a game of Simon Says. Instead of repeating the actions Simon Says once, a loop in a computer program repeats a set of actions over and over until it's told to stop. Just like in Simon Says, we start with a command, do it, then do the next command, then the next, then we start all over again until Simon says to stop.

Imagine loop in JavaScript

Using Loops in JavaScript

In JavaScript, which is a language that websites use to do cool things, loops are used to make things happen many times. It's like if you wanted your digital pet to jump 10 times. You would use a loop to tell the pet to keep jumping until it has jumped 10 times.

Example of a Loop in JavaScript

Let's say we want to make the digital pet jump 10 times. Could you imagine what would happen if we typed 'jump' command 10 times? The code would be very long and would look confusing. Instead, we can tell the computer 'Repeat 'jump' 10 times!' and the computer will understand us.

for (var i = 0; i < 10; i++) {
  digitalPet.jump();
}

Breaking Down the Code

The 'for' in the code is like saying 'For each time'. The 'var i = 0;' is like starting a countdown from 0. The 'i < 10;' is like saying 'as long as i is less than 10.' The 'i++' is saying 'add 1 to i after each turn'. And 'digitalPet.jump();' is the command we want to repeat 10 times. So, overall, the computer reads it as 'For each time starting from 0, as long as 'i' is less than 10, add 1 to 'i' after each turn and make digitalPet jump.'

When does the Loop stop?

The loop stops when 'i' is not less than 10 anymore. Imagine if you have 10 candies and each time you eat one, you count. Once you have eaten 10 candies, you won't have any more candies left and you'll stop eating. That's the same concept with loops, once 'i' is not less than 10, the loop stops.

No Time to Read? Learn on the Go!

By reading this article, you've invested 1.72 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.

Loops can also be controlled with break and continue statements, for more details see Control flow in JavaScript

About author

Roman Y.
Senior Software Engineer at Nike

Why did I decide to launch this website? Drawing from my rich background in interviewing candidates for a variety of developer roles, I've observed a common pattern: many applicants share similar deficiencies in their knowledge during technical interviews. Motivated by this insight, I established this website with the aim of assisting developers in securing their ideal job. Through straightforward and concise articles, my goal is to not only deepen your understanding of programming language nuances but also to equip you with the insights needed to deliver the precise answers interviewers expect. Essentially, you'll be providing the correct response. I encourage you to spread the word about this site on social media platforms. Echoing the wisdom of an Armenian saying: "Do good and cast it into the water."

EXCLUSIVELY

Certain articles only distributed to subscribers through email