J

JavaScript Handbook

Clean • Professional

JavaScript Loops — Top Interview Questions & Answers

3 minute

JavaScript Loops — Interview Questions & Answers

Ques: What are loops in JavaScript?

Ans: Loops are used to execute a block of code repeatedly until a certain condition is met. They help reduce repetition and make the code cleaner.

Common loop types in JavaScript are:

  • for
  • while
  • do...while
  • for...in
  • for...of

Ques: What is the syntax of a for loop?

Ans: The for loop runs a block of code a specific number of times.

Syntax:

for (initialization; condition; increment) {
  // code to execute
}

Example:

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

Output:

1
2
3
4
5

Ques: How does a while loop work?

Ans: A while loop runs as long as the condition is true. It checks the condition before each iteration.

Example:

let count = 1;
while (count <= 3) {
  console.log("Hello");
  count++;
}

Output:

Hello
Hello
Hello

Ques: What is a do...while loop?

Ans: The do...while loop executes at least once, even if the condition is false, because the condition is checked after the first execution.

Example:

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

Output:

5

Ques: What is the difference between while and do...while loops?

Featurewhiledo...while
Condition CheckBefore the loop startsAfter one iteration
Minimum Executions01
Use CaseWhen condition might be false initiallyWhen code must run at least once

Ques: What is the for...in loop used for?

Ans: The for...in loop is used to iterate over object properties (keys).

Example:

const person = { name: "Alice", age: 25 };
for (let key in person) {
  console.log(key + ": " + person[key]);
}

Output:

name: Alice
age: 25

Ques: What is the for...of loop used for?

Ans: The for...of loop is used to iterate over iterable objects like arrays, strings, maps, etc.

Example:

const colors = ["red", "green", "blue"];
for (let color of colors) {
  console.log(color);
}

Output:

red
green
blue

Ques: What is the difference between for...in and for...of?

Featurefor...infor...of
Iterates overProperty names (keys)Values
Used forObjectsArrays, strings, iterables
Examplefor (let key in obj)for (let value of arr)

Ques: What are break and continue statements in loops?

  • break → Exits the loop completely.
  • continue → Skips the current iteration and moves to the next one.

Example:

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

Output:

1
2
4

Ques : Can loops be nested in JavaScript?

Ans: Yes, You can place a loop inside another loop — called a nested loop.

Example:

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

Output:

i=1, j=1
i=1, j=2
i=1, j=3
i=2, j=1
i=2, j=2
i=2, j=3

Ques : What are infinite loops?

Ans: An infinite loop occurs when the condition never becomes false, causing the loop to run endlessly.

Example:

while (true) {
  console.log("Running forever...");
}

 

Article 0 of 0