We use cookies on this website to make it function correctly and to achieve the purposes illustrated in the cookie policy. By continuing to browse, you agree to the use of cookies. Cookie Policy

JavaScript Loops: For-In, While Loops & More

Bianca Gandolfo

The Different Types of JavaScript Loops Explained: For-In, While Loops & More

Hack Reactor places an emphasis on JavaScript because it's the most valuable and important programming language used today. (You can find some great resources for learning JavaScript here, via StackOverFlow.)

For this blog post, we're going to focus on JavaScript loops.

Explaining Various JavaScript Loops: For-in Loops, While Loops & More

What is the difference between a for loop and a while loop? And what about the for-in, do-while and for-each?

This is a question I get a lot from beginning JavaScripters that come to my meetups! For loops and while loops are very similar, which is why it is easy to get confused about when to use one over the other. You can theoretically use them interchangeably, but here are a few best practice guidelines. For-in, for-each and do-while JavaScript loops are more specialized and easier to differentiate, but I will include them just to cover all the bases.

For Loop

For Loops: When you know how many times you want to loop. When you are iterating through the indices of an array. When you have some sort of counter.

For-In Loop

For-In Loops: When you are iterating over the properties of an object.

For-Each Loop

For-Each: When you want to iterate over the values of an object's properties.

While Loop

While Loops: When you may be unsure of the number of times to loop.
When you want to loop while some condition is true.

Do-While Loop

Do-While Loops: When you want it to loop at least once before checking if the condition is true.

Can you think of any good rules of thumb for when to use these JavaScript loops? Watch these videos about for loops and while loops below!

Intro to While Loops: Transcription

There are a few different types of loops in JavaScript. In this video I'm going to be specifically talking about the while loops.

For those who don't know what a JavaScript loop is, let me explain. As programmers, we're really lazy. We like to work smarter, not harder. What a loop does is it allows us to run code as many times as we want, repeatedly, without having to type that line of code in every time.

Let's say I wanted to write something out on the screen ten times. I could go ahead and be boring and type out document.write and type in whatever, like "this is a sentence" or something.

I could copy and paste it ten times and that would be fine. It would run. But that's not very efficient. I've wasted ten lines of code in my text editor.

A much smarter way of doing things is to run a Javascript loop.

(For the rest of Quentin's tutorial, watch the video above. The author of this post, Bianca Gandolfo, is a full-stack engineer from Hack Reactor. She previously worked a Visual Stager. Read more from Bianca at her personal blog.  Tweet your JavaScript questions to @HackReactor and we'll do our best to respond! Also, check out our latest article on JavaScript variables.)