Wednesday, 22 January 2025

JavaScript for Loops: A Beginner Guide with Examples

A for loop in JavaScript is a control flow statement that allows you to repeat a block of code a specific number of times. It is one of the most commonly used loops in programming and is particularly useful when you know in advance how many times you want to execute a statement or a block of statements.

Basic Syntax

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

·      initialization: This step is executed once before the loop starts. It is typically used to initialize one or more loop counters. This is an optional expression.

 

·      condition: The loop continues to execute as long as this condition is true. If it evaluates to false, the loop stops. This is also an optional expression.

 

·      increment/decrement: This step is executed after each iteration of the loop. It is typically used to increment or decrement the loop counter(s). This is another optional expression.

 

Example 1: Print numbers from 1 to 5.

 

printNumbers.js

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

Output

1
2
3
4
5

·      let i = 1;: Initializes the counter i to 1.

·      i <= 5;: The loop will continue as long as i is less than or equal to 5.

·      i++: After each iteration, the value of i is increased by 1.

 

Example 2: Looping Through an Array.

 

arrayTraversal.js

const fruits = ['apple', 'banana', 'orange', 'mango'];

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

Output

apple
banana
orange
mango

·      let i = 0;: The loop starts with i at 0, which is the first index of the array.

·      i < fruits.length;: The loop continues as long as i is less than the length of the fruits array.

·      fruits[i]: Accesses the element at the index i of the fruits array.

 

Example 3: Summing Numbers in an Array

 

sumOfNumbers.js

const numbers = [10, 20, 30, 40, 50];
let sum = 0;

for (let i = 0; i < numbers.length; i++) {
  sum += numbers[i];
}

console.log('Total sum:', sum);

Output

Total sum: 150

·      The loop iterates over each element in the numbers array.

·      sum += numbers[i]; adds the current number to the sum variable.

 

Example 4: Looping Backwards

 

printBackwards.js

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

Output

5
4
3
2
1

·      let i = 5;: Initializes the counter i to 5.

·      i > 0;: The loop continues as long as i is greater than 0.

·      i--: After each iteration, the value of i is decreased by 1.

 

Example 5: Breaking Out of a For Loop

 

breakLoop.js

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

Output

1
2
3
4
5

·      The loop is set to run from 1 to 10.

·      The break statement is used to exit the loop when i equals 6.

 

Example 6: Skipping an Iteration with continue.

 

continueStmt.js

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

Output

1
2
4
5

The loop skips the iteration where i equals 3 due to the continue statement.


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment