Contact Us

Email: info@mohitdesigns.com
Mobile: +91-9718991639

javascript loops

Mastering JavaScript Loops: A Beginner’s Guide

  • Sterilization function set at 80 degrees to ensure a safe and hygienic experience.
  • SS316 Heating Element for faster heating
  • This smart water heater enables 20% more hot water utilization and energy efficiency with its Performance Based Heattech…
11,499.00 INR
Is prime

JavaScript, one of the most popular programming languages in the world, provides various loop constructs to help developers execute a block of code multiple times. Loops are a fundamental concept in programming, enabling automation and reducing redundancy. In this blog post, we’ll explore the most commonly used javascript loops in JavaScript, along with examples to demonstrate their usage.

JavaScript Loops:

1. for Loop

The for loop is the most commonly used loop in JavaScript. It repeats a block of code a specific number of times, making it ideal when the number of iterations is known beforehand.

Syntax:

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

Example:

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

Explanation: This loop starts with i set to 0 and runs as long as i is less than 5. After each iteration, i is incremented by 1.

2. while Loop

The while loop repeats a block of code as long as a specified condition is true. It’s particularly useful when the number of iterations is not known in advance.

Syntax:

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

Example:

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

Explanation: This loop runs as long as i is less than 5. The variable i is incremented by 1 after each iteration.

3. do…while Loop

The do…while loop is similar to the while loop, but with one key difference: the block of code inside the loop is executed at least once before the condition is tested.

Syntax:

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

Example:

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

Explanation: The loop executes the code block once before checking if i is less than 5. If the condition is true, the loop continues.

4. for…of Loop

The for…of loop is designed to iterate over iterable objects like arrays, strings, and more. It’s a more readable and concise way to loop through the elements of an array or any iterable.

Syntax:

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

Example:

let fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
    console.log(fruit);
}

Explanation: This loop iterates over each element in the fruits array, logging each fruit to the console.

5. for…in Loop

The for…in loop is used to iterate over the properties of an object. It should be used with caution when looping through arrays, as it iterates over the array’s properties rather than the values.

Syntax:

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

Example:

let person = { name: "John", age: 30, city: "New York" };
for (let key in person) {
    console.log(key + ": " + person[key]);
}

Explanation: This loop iterates over the properties of the person object, logging each key-value pair to the console.

6. break and continue Statements

Although not loops themselves, the break and continue statements are often used in conjunction with loops to control the flow of execution.

  • break: Exits the loop immediately.
  • continue: Skips the current iteration and proceeds to the next one.

Example using break:

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

Explanation: The loop stops when i equals 5, exiting the loop.

Example using continue:

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

Explanation: When i equals 3, the continue statement skips that iteration, so 3 is not logged.

Conclusion

Understanding and utilizing loops in JavaScript is essential for writing efficient and concise code. Each loop type has its own strengths and is best suited for different scenarios. Whether you’re iterating through an array, repeating a task a set number of times, or working with object properties, JavaScript provides a loop for the job. By mastering these loops, you’ll be able to streamline your code and enhance your programming skills.