Mark As Completed Discussion

Loops

Loops are used in JavaScript to repeatedly execute a block of code. They allow you to perform repetitive tasks efficiently without writing the same code over and over. There are several types of loops in JavaScript, including the for loop, while loop, and do...while loop.

for loop

The for loop is commonly used when you know the number of times you want to repeat a block of code. It consists of three parts: the initialization, condition, and iteration. Here's the syntax of a for loop:

JAVASCRIPT
1for (initialization; condition; iteration) {
2  // code to be executed on each iteration
3}

For example, let's say you want to print the numbers from 1 to 100. You can use a for loop to achieve this:

JAVASCRIPT
1for (let i = 1; i <= 100; i++) {
2  console.log(i);
3}

In this example, the variable i is initialized with a value of 1. The loop will continue as long as the condition i <= 100 is true. After each iteration, the value of i is incremented by 1.

Here's another example that demonstrates the use of a for loop to implement the FizzBuzz problem:

JAVASCRIPT
1// Loop through numbers from 1 to 100
2for (let i = 1; i <= 100; i++) {
3  // Check if the number is divisible by 3 and 5
4  if (i % 3 === 0 && i % 5 === 0) {
5    console.log('FizzBuzz');
6  }
7  // Check if the number is divisible by 3
8  else if (i % 3 === 0) {
9    console.log('Fizz');
10  }
11  // Check if the number is divisible by 5
12  else if (i % 5 === 0) {
13    console.log('Buzz');
14  }
15  // Otherwise, log the number
16  else {
17    console.log(i);
18  }
19}

In this example, the loop iterates through numbers from 1 to 100. For each number, it checks if the number is divisible by 3 and 5, divisible by 3 only, divisible by 5 only, or none of the above, and logs the corresponding message.

while loop

The while loop is used when you don't know the number of times you want to repeat a block of code, but you have a condition that needs to be tested before each iteration. Here's the syntax of a while loop:

JAVASCRIPT
1while (condition) {
2  // code to be executed on each iteration
3}

For example, let's say you want to print the numbers from 1 to 10 using a while loop:

JAVASCRIPT
1let i = 1;
2
3while (i <= 10) {
4  console.log(i);
5  i++;
6}

In this example, the variable i is initially set to 1. The loop will continue as long as the condition i <= 10 is true. After each iteration, the value of i is incremented by 1.

do...while loop

The do...while loop is similar to the while loop, but the condition is tested after each iteration. This means that the code block will always be executed at least once, regardless of whether the condition is true or false. Here's the syntax of a do...while loop:

JAVASCRIPT
1do {
2  // code to be executed on each iteration
3} while (condition);

For example, let's say you want to prompt the user for a number between 1 and 10, and keep prompting until a valid number is entered using a do...while loop:

JAVASCRIPT
1let number;
2
3do {
4  number = prompt('Enter a number between 1 and 10:');
5} while (number < 1 || number > 10);
6
7console.log('Valid number entered: ' + number);

In this example, the code block prompts the user for a number and assigns it to the number variable. The loop will continue as long as the condition number < 1 || number > 10 is true. After each iteration, the condition is checked, and if it's still true, the code block is executed again.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment