JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Loop For


📘 JavaScript for Loop – Repeat Tasks with Control

A for loop allows you to execute a block of code a fixed number of times. It's especially useful when you know in advance how many times to run the loop.


🔹 Syntax

for (initialization; condition; increment) {
  // code block to be executed
}

Components:

  • Initialization – executed once before the loop starts.

  • Condition – checked before each iteration.

  • Increment – executed after each iteration.


🔹 Basic Example

for (let i = 1; i <= 5; i++) {
  console.log("Number:", i);
}
// Output: 1 2 3 4 5

🔹 Looping through Arrays

let fruits = ["Apple", "Banana", "Mango"];

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

🔹 Using break and continue

for (let i = 1; i <= 5; i++) {
  if (i === 3) break;
  console.log(i);
}
// Output: 1 2
for (let i = 1; i <= 5; i++) {
  if (i === 3) continue;
  console.log(i);
}
// Output: 1 2 4 5

🔹 Nested for Loop

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

Practice Questions

Q1. How do you print numbers from 1 to 10 using a for loop?

Q2. How can you loop through an array of strings using a for loop?

Q3. What is the role of the increment statement in a for loop?

Q4. How can you skip a particular iteration in a for loop using continue?

Q5. What happens if you forget to update the loop counter in a for loop?

Q6. How can you exit a for loop prematurely using break?

Q7. Write a for loop that prints only even numbers from 1 to 10.

Q8. How can you use a for loop to count backwards from 10 to 1?

Q9. Can you use a for loop to loop through a string’s characters? How?

Q10. Write a nested for loop to generate pairs like (1,1), (1,2), (2,1), (2,2).


JS Loop For Quiz

Q1: What is the correct syntax for a for loop?

A. for (i to 5)
B. loop (i = 0; i < 5; i++)
C. for (let i = 0; i < 5; i++)
D. foreach i in 5

Q2: Which part of the for loop is executed only once?

A. Initialization
B. Condition
C. Increment
D. All of the above

Q3: What is the purpose of the break keyword in a loop?

A. Restart the loop
B. Skip the current iteration
C. Exit the loop immediately
D. Pause the loop

Q4: What happens if the loop condition is always true?

A. Loop runs once
B. Syntax error
C. Infinite loop
D. Runtime warning

Q5: How do you skip the current iteration in a loop?

A. exit
B. pass
C. continue
D. skip

Q6: Which of the following is a valid for loop to print only even numbers from 2 to 10?

A. for (i = 2; i < 10; i++)
B. for (i = 2; i <= 10; i+=2)
C. for (i = 2; i <= 10; i--)
D. for (i = 1; i <= 10; i+=2)

Q7: How many times will this loop run? for (let i = 0; i <= 3; i++) {}

A. 3
B. 4
C. 5
D. 2

Go Back Top