Menu

Mastering JavaScript Loops: For, For...Of, and While Explained

By TechQuest eAcademy · 10/24/2024

👀 64 views👍 4 likes💬 1 comments0 favorites

Key Points

  • Learn about three essential loops in JavaScript: for, for...of, and while.
  • Understand when to use each loop for efficient coding.
  • Explore practical examples to solidify your understanding.

Introduction

In this video, we dive deep into the world of loops in JavaScript, exploring three essential types: for, for...of, and while. Understanding when to use each type is crucial for writing efficient and effective code.

What is a Loop?

A loop is a programming construct that allows you to execute the same block of code multiple times. It is useful for:

  • Repeating tasks without rewriting code.
  • Iterating through collections like arrays or strings.
  • Continuing execution as long as a specified condition remains true. Learn more.

The For Loop

The for loop is ideal when you know exactly how many times you want to execute a block of code. It provides complete control over the iteration process. The basic syntax includes initialization, condition, and increment. For example, counting from one to five:

for (let i = 1; i <= 5; i++) {
    console.log(`Number ${i}`);
}

This will print numbers one through five. See the example.

Looping Through an Array

You can also use a for loop to iterate through an array. For instance, if you have an array of fruits:

const fruits = ['apple', 'banana', 'cherry', 'date'];
for (let i = 0; i < fruits.length; i++) {
    console.log(`Fruit at index ${i} is ${fruits[i]}`);
}

This will print each fruit in the array. Check it out.

The For...Of Loop

The for...of loop is a cleaner way to iterate over iterable objects like arrays and strings. It allows you to access values directly without needing the index. For example:

for (const fruit of fruits) {
    console.log(`Fruit: ${fruit}`);
}

This will print each fruit directly. Learn more about for...of.

Iterating Through a String

You can also use the for...of loop to iterate through a string:

const name = 'John';
for (const letter of name) {
    console.log(letter);
}

This will print each letter in the name. See the string example.

The While Loop

The while loop continues to execute as long as the specified condition remains true. Use this loop when the number of iterations is not known beforehand. For example, counting down from five:

let i = 5;
while (i > 0) {
    console.log(i);
    i--;
}

This will print numbers from five down to one. Explore while loop.

Summary

We covered three types of loops: for, for...of, and while. Each loop serves different purposes:

  • Use for loop when you know the exact number of iterations.
  • Use for...of loop for cleaner iteration over values.
  • Use while loop when the number of iterations is dynamic. Review best practices.

Thanks for watching! If you have any questions, feel free to drop them in the comments below. Subscribe for more tutorials.

You Might Also Like