Projects

JavaScript Loops

Introduction

Hi there! Today, we're going to learn about loops in JavaScript. Loops are a way to repeat a piece of code several times. This is really helpful when we need to do the same thing over and over again, like going through a list of items or doing a repeated task.

Imagine you have a list of tasks to do. Instead of writing the same code for each task, you can use a loop to do it for you. There are different types of loops in JavaScript, and each one has its own way of working. Let's take a look at them.

for Loops

The for loop is one of the most common loops in JavaScript. It lets us run a piece of code a set number of times. The for loop has three main parts: initialization, condition, and increment. Let's break it down:


for (initialization; condition; increment) {
// Code to be executed
}

Think of a for loop like a countdown timer. Let's say you set the timer to 10 seconds. The initialization is setting the timer to 10. The condition is checking if the timer is above 0, and the increment is counting down by 1 second each time.

Here's an example that prints the numbers from 1 to 5 using a for loop:


for (let i = 1; i <= 5; i++) {
console.log(i);
}
// Output: 1, 2, 3, 4, 5

In this example, the loop starts with i set to 1. The loop runs as long as i is less than or equal to 5. After each loop, i is increased by 1. The loop prints the value of i in each round.

Breaking it down:

while Loops

The while loop is another type of loop. It keeps running as long as a condition is true. Unlike the for loop, the while loop only has a condition. Let's break it down:


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

The while loop works like this:

Think of a while loop like a traffic light. The light stays green as long as there are no cars waiting. The condition is whether there are cars waiting. If the condition is true (no cars waiting), the light stays green.

Here's an example that prints the numbers from 1 to 5 using a while loop:


let i = 1;

while (i <= 5) {
console.log(i);
i++;
}
// Output: 1, 2, 3, 4, 5

In this example, the loop starts with i set to 1. The loop runs as long as i is less than or equal to 5. After each loop, i is increased by 1. The loop prints the value of i in each round.

Breaking it down:

do-while Loops

The do-while loop is like the while loop, but it always runs the code block at least once before checking the condition. Let's break it down:


do {
// Code to be executed
} while (condition);

The do-while loop works like this:

Think of a do-while loop like eating your favorite candy. You will eat the candy at least once, and then check if you have more. If you do, you eat another piece. The code block is eating the candy, and the condition is checking if there's more.

Here's an example that prints the numbers from 1 to 5 using a do-while loop:


let i = 1;

do {
console.log(i);
i++;
} while (i <= 5);
// Output: 1, 2, 3, 4, 5

In this example, the code block inside the do section runs first, printing the value of i. After that, the condition is checked. If it's true, the loop runs again.

Breaking it down:

for...in Loops

The for...in loop is used to go through the properties of an object. This loop is useful when you want to access the keys of an object. Let's break it down:


for (key in object) {
// Code to be executed
}

The for...in loop works like this:

Think of a for...in loop like looking through the drawers of a dresser. Each drawer is a key, and the contents inside the drawer are the values. You open each drawer (key) one by one and see what's inside (value).

Here's an example where we go through the properties of an object using a for...in loop:


const person = {
name: 'Alice',
age: 30,
city: 'New York'
};

for (let key in person) {
console.log(key + ': ' + person[key]);
}
// Output:
// name: Alice
// age: 30
// city: New York

In this example, the loop goes through each key in the person object and prints the key along with its value.

Breaking it down:

for...of Loops

The for...of loop is used to go through iterable objects like arrays, strings, and sets. This loop is useful when you want to access the values of an iterable. Let's break it down:


for (element of iterable) {
// Code to be executed
}

The for...of loop works like this:

Think of a for...of loop like reading a book. Each page is an element, and you go through each page one by one. The loop variable is the current page you are reading.

Here's an example where we go through an array using a for...of loop:


const numbers = [1, 2, 3, 4, 5];

for (let number of numbers) {
console.log(number);
}
// Output: 1, 2, 3, 4, 5

In this example, the loop goes through each value in the numbers array and prints the value.

Breaking it down:

Conclusion

Loops are a powerful and essential part of JavaScript. They help us do repetitive tasks and handle collections of data easily. By learning about the different types of loops and how they work, you'll be able to write better and shorter code.

Try using these loops in your own projects and see how they work in different situations. Happy coding!