DEV Community

Cover image for JavaScript Loops Explained: A Beginner's Guide to For, While, and More
WISDOMUDO
WISDOMUDO

Posted on

JavaScript Loops Explained: A Beginner's Guide to For, While, and More

Introduction

Loops are a core concept in JavaScript that allow you to run a block of code repeatedly, making it easier to handle repetitive tasks efficiently. Whether you're handling arrays, iterating over object properties, or repeating a task until a specific condition is met, loops make it possible.

In this beginner-friendly guide, you’ll learn the different types of loops in JavaScript, when to use them, and how they can be applied to solve real-world problems. You'll finish this guide with the confidence to write optimized and well-structured code.

What We’ll Cover

Here, we’ll walk you through the following loop types:

  1. for loop
  2. while loop
  3. do...while loop
  4. for...in loop
  5. for...of loop

for Loop

A for loop is typically used when you have a specific number of iterations in mind, and it consists of three main parts.

Syntax

for (initialization; condition; increment) {
// code to run
}
Enter fullscreen mode Exit fullscreen mode

Real-Life Example:

for (let i = 1; i <= 6; i++) {
console.log("Number:", i);
}
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Starts from i = 1
  • Runs while i <= 6
  • Increases i by 1 after each loop
  • Prints numbers 1 to 6

while Loop

A while loop runs as long as a specified condition remains true, making it ideal when the loop count isn't predetermined.

Syntax

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

Real-Life Example:

let i = 1;
while (i <= 6) {
console.log("Count:", i);
i++;
}
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Checks the condition i <= 6 before each loop
  • Runs as long as the condition is true
  • Increases i each time

do...while Loop

Even if the condition is false at first, this loop ensures the code runs once before any checks.

Syntax

do {
// code to run
} while (condition);
Enter fullscreen mode Exit fullscreen mode

Real-Life Example:

let i = 1;
do {
console.log("Step:", i);
i++;
} while (i <= 6);
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Executes the code block once first
  • Then checks the condition
  • Repeats the block if the condition is still true

for...in Loop (for Objects)

When you want to iterate over the keys of an object, the for...in loop is the ideal choice.

Syntax

for (let key in object) {
// code to run
}
Enter fullscreen mode Exit fullscreen mode

Real-Life Example:

for (let key in user) {
console.log(key + ":", user[key]);
}
const user = {
name: "Wisdom",
age: 30,
city: "Port Harcourt"
};

Enter fullscreen mode Exit fullscreen mode

What it does:

  • Loops through each key in the object
  • Accesses the value using user[key]

for...of Loop (for Iterables)

This loop is ideal for looping through elements in iterables, including arrays, strings, maps, and sets.

Syntax

for (let value of iterable) {
// code to run
}
Enter fullscreen mode Exit fullscreen mode

Real-Life Example:

const fruits = ["apple", "banana", "orange"];
for (let fruit of fruits) {
console.log(fruit);
}
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Loops through each item in the array
  • Directly accesses each value without using an index

Conclusion

Understanding loops is a vital step in becoming proficient with JavaScript. Whether you're iterating over arrays, processing user data, or working with object properties, loops give you the control and flexibility to handle repetitive tasks efficiently.

By learning the differences between for, while, do...while, for...in, and for...of, you gain the tools to write cleaner, more dynamic code that adapts to a variety of real-world scenarios. These loop structures are not only foundational in JavaScript but also form the basis of logical thinking and flow control in programming.

As you continue your journey in web development, make it a habit to practice and experiment with these loops in small projects. Over time, choosing the right loop will become second nature, and your ability to solve problems with code will improve significantly.

Happy coding!

You can reach out to me via LinkedIn

Top comments (0)