$100 Website Offer

Get your personal website + domain for just $100.

Limited Time Offer!

Claim Your Website Now

JavaScript Loops

In JavaScript, loops are used to execute a block of code repeatedly until a specified condition is met. There are several types of loops available:

1. for Loop:

The for loop repeats a block of code a specified number of times.

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

2. while Loop:

The while loop repeats a block of code while a specified condition is true.

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

3. do…while Loop:

The do...while loop is similar to the while loop, but it executes the block of code at least once before checking the condition.

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

Loop Control Statements:

JavaScript provides loop control statements to control the flow of loops:

  • break: Terminates the loop immediately.
  • continue: Skips the current iteration of the loop and continues with the next iteration.
for (let i = 0; i < 10; i++) {
  if (i === 5) {
    break; // Exit the loop when i is 5
  }
  console.log(i);
}
for (let i = 0; i < 10; i++) {
  if (i === 5) {
    continue; // Skip iteration when i is 5
  }
  console.log(i);
}

Nested Loops:

You can nest loops within each other to perform more complex iterations.

for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 2; j++) {
    console.log(`i: ${i}, j: ${j}`);
  }
}

Looping Through Arrays:

You can use loops to iterate over arrays and perform operations on each element.

let fruits = ['apple', 'banana', 'orange'];

for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}

forEach() Method:

The forEach() method is a built-in method for arrays that executes a provided function once for each array element.

fruits.forEach(function(fruit) {
  console.log(fruit);
});

Loops are essential for performing repetitive tasks and iterating over collections of data in JavaScript. Depending on the situation, you can choose the appropriate type of loop to achieve your desired outcome efficiently.

Related Posts

Exploring Dining Options Made Simple: How to Discover Restaurants and Connect with Venues

Introduction Deciding where to eat can sometimes feel surprisingly difficult. Whether you are looking for a quick lunch during a busy workday, planning a celebratory family dinner,…

Read More

Elevate Your Daily Research and Industry Insights With a Smart AI News Aggregator

Introduction Following the news today can feel like standing in the middle of a busy railway station while dozens of announcers shout different updates at the exact…

Read More

Website Contact Page Design: Best Practices for Usability, Trust, and Conversions

Introduction A website contact page is often the final destination before a prospect decides whether to do business with you. Potential buyers navigate through your service pages,…

Read More

Smart Travel Connectivity: How to Evaluate Data, Networks, and Roaming Plans

Introduction Stepping off an international flight without internet access creates immediate hurdles. Modern travelers depend on connected phones to hail airport rides, check digital boarding passes, navigate…

Read More

Mautic vs listmonk vs Dittofeed — Complete Self-Hosted Marketing Automation Comparison

For your requirements—automatic lead capture, centralized contacts, segmentation, Amazon SES, WhatsApp bulk campaigns, automated follow-ups, low operating costs, and complete data ownership—Mautic is my first recommendation. However,…

Read More

Global Mobile Network Identifiers: A Practical Guide to Telecom Codes and Provider Records

Introduction Every time a mobile phone connects to a cellular tower, a quiet digital handshake takes place. Whether a smartphone is roaming in a foreign airport, an…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x