Skip to main content

While & For Loops - JavaScript Interview

JavaScript Fundamentals: While & For Loops




In Simple terms, what are loops used for in JavaScript?

View Answer:
Interview Response: Loops in JavaScript are used to execute a block of code repeatedly until a certain condition is met or a specified number of iterations have been completed.

Can you explain how a while loop works?

View Answer:
Interview Response: A while loop in JavaScript repeatedly executes a block of code as long as a specified condition remains true, and stops when the condition becomes false.


Code Example:

let i = 0;
while (i < 3) {
// shows 0, then 1, then 2
console.log(i);
i++;
}

What is the definition of an iteration in a JavaScript loop?

View Answer:
Interview Response: An iteration in a JavaScript loop refers to each individual execution of the loop's body, typically corresponding to one cycle of the loop.

Code Example: Below, we see three iterations in the while loop body.

let i = 1;
while (i < 3) {
// shows 1, then 2, then 3
console.log(i);
i++;
}

Does a while loop require an explicit incrementor?

View Answer:
Interview Response: A while loop does not require an explicit incrementor. However, it is crucial to ensure the loop condition eventually becomes false to avoid infinite loops.

Code Example:

// With Incrementor
let i = 3;
while (i) {
// shows 0, then 1, then 2
console.log(i);
i--; // if the incrementor (i--) is missing then it results in an endless loop.
}

// Without Incrementor
// Here's an example of a while loop without an explicit incrementor:
let items = ['apple', 'banana', 'orange'];
while (items.length > 0) {
console.log(items.pop());
}

// This loop removes and logs each item until the array is empty.

Are curly brackets required in a single-line loop body?

View Answer:
Interview Response: Curly brackets are not required for a single-line loop body in languages like C, C++, Java, or JavaScript. However, they improve readability and prevent errors.

Code Example:

let i = 3;
while (i) console.log(i--);

What is the difference between a Do-While and a While Loop?

View Answer:
Interview Response: The main difference is that a do-while loop executes its body at least once before checking the condition. A While loop, on the other hand, only executes its loop body if the loop condition is initially true.

Code Example:

do {
// loop body
} while (condition);

Can you explain how the do-while loop works in JavaScript?

View Answer:
Interview Response: The loop first executes the body, checks the condition, and executes it again while it is truthy.

Code Example:

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

How does a for-loop function in JavaScript?

View Answer:
Interview Response: The for loop defines an initial point, condition, and steps. The initial point runs once when entering the loop. Before each iteration, the condition is evaluated. The loop body repeats while the condition remains true; if false, the loop then exits before the next iteration.

The general loop algorithm works like this:

Run begin

  • (if condition → run body and run step)
  • (if condition → run body and run step)
  • (if condition → run body and run step)
  • ...

Code Example:

// for (let i = 0; i < 3; i++) console.log(i)

// run begin
let i = 0;
// if condition → run body and run step
if (i < 3) {
console.log(i);
i++;
}
// if condition → run body and run step
if (i < 3) {
console.log(i);
i++;
}
// if condition → run body and run step
if (i < 3) {
console.log(i);
i++;
}
// ...finish, because now i == 3

What is a for-Loop inline variable declaration?

View Answer:
Interview Response: In JavaScript, an inline variable declaration in a for-loop initializes the loop control variable directly within the loop statement, leaving it within the loop's scope.

Technical Response: Inline variable declaration is the process of declaring a variable starting point inside of the for-loop. The variable is only visible inside the loop and cannot be accessed globally.


Code Example:

for (let i = 0; i < 3; i++) {
console.log(i); // 0, 1, 2
}

console.log(i); // error, no such variable

Code Example: Variable declaration outside of the loop

let i = 0;

for (i = 0; i < 3; i++) {
// use an existing variable
console.log(i); // 0, 1, 2
}

console.log(i); // 3, visible, because declared outside of the loop

Is it possible to skip or omit parts of the for-loop settings?

View Answer:
Interview Response: It is possible to skip any or all parts of the for-loop in JavaScript. However, omitting all parts will create an infinite loop, and semicolons must still be included to avoid syntax errors.

Technical Response: Yes, it is possible to omit parts or all the for-loop settings. If you remove all the parts, it results in an endless loop. Please note that the two semicolons (;) must be present, and otherwise, there would be a syntax error.

Code Example:

let i = 0; // we have i already declared and assigned

for (; i < 3; i++) {
// no need for "start"
console.log(i); // 0, 1, 2
}

Can you stop a loop based on a specific condition?

View Answer:
Interview Response: In JavaScript, you can stop a loop, based on a specific condition, by placing a 'break' statement inside an 'if' block that evaluates the desired condition within the loop body.

Technical Response: Yes, You can achieve this by using the break directive or statement to stop the loop at any time. This approach is ideal for cases when you need to pause the loop in the middle or at various points along its length. The break directive works with all traditional looping structures. (It does not work with forEach).


Code Example: While Loop - Stopping the Loop when a number does not get entered.

let sum = 0;

while (true) {
let value = +prompt('Enter a number', '');

if (!value) break; // (*)

sum += value;
}

console.log('Sum: ' + sum);
Code Example: For Loop

let text = '';
for (let i = 0; i < 10; i++) {
if (i === 3) {
break;
}
text += 'The number is ' + i + '<br>';
}

document.getElementById('demo').innerHTML = text;

// Output:
// A loop with a break statement.

// The number is 0
// The number is 1
// The number is 2

How does the continue directive work in a loop?

View Answer:
Interview Response: In JavaScript, the 'continue' directive skips the current iteration of a loop and proceeds to the next. It doesn't stop the entire loop, just the current iteration.

Technical Response: The continue directive is a "lighter version" of the break statement. It does not stop the whole loop; instead, it stops the current iteration and forces the loop to start a new one (if the condition allows).


Code Example: The Loop uses the continue statement to output odd values.

for (let i = 0; i < 10; i++) {
// if true, skip the remaining part of the body
if (i % 2 == 0) continue;

console.log(i); // 1, then 3, 5, 7, 9
}

What is the difference between the break statement and the continue directive?

View Answer:
Interview Response: In JavaScript, The break statement terminates the entire loop, while the continue directive skips the current iteration and proceeds to the next iteration in the loop.

Technical Response: The break statement stops the loop in the middle or several places of its body. The continue directive is a “lighter version” of break. It does not stop the whole loop; instead, it stops the current iteration and forces the loop to start a new one (if the condition allows).


What is a potential benefit of using the continue directive?

View Answer:
Interview Response: In JavaScript, using the continue directive can potentially improve the performance of a loop by skipping unnecessary iterations. The continue directive also helps decrease nesting and increases code readability.

Can the continue or break directives be used with the shorthand ternary (?:) expression?

View Answer:
Interview Response: No, continue and break directives cannot be used with the shorthand ternary (?:) expression, as they require proper statement context within a loop.

Technical Response: No, the continue or break directives on a ternary operator's right side (?) results in a syntax error.


Code Example:

// proper of the continue directive in a conditional
if (i > 5) {
console.log(i);
} else {
continue; // continue is allowed here
}

// continue is not allowed on the right side of the question mark operator (?)
(i > 5) ? console.log(i) : continue;

How do you break out of two nested for loops?

View Answer:
Interview Response: You can use a label to break out of two nested for-loops, while the break directive targets the label to ensure that both loops halt.

Code Example:

outer: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
let input = prompt(`Value at coordinates (${i},${j})`, '');

// if an empty string or canceled, then break out of both loops
if (!input) break outer; // (*)

// do something with the value...
}
}

console.log('Done!');

Can labels jump to an arbitrary place in the code?

View Answer:
Interview Response: In JavaScript, labels cannot jump to arbitrary places in your code. They are used with break and continue statements, specifically for loops.

Technical Response: No, Labels do not allow us to jump into an arbitrary place in the code. A call to break/continue is only possible from inside a loop, and the label must be somewhere above the directive.

Code Example:

break label; // doesn't jump to the label below

label: for (...)

What is the difference between the While and For loops in JavaScript?

View Answer:
Interview Response: In JavaScript, while-loops check a condition before execution, while for-loops have an initialization, condition, and update expression within the loop statement.

What is an infinite loop in JavaScript, and how can it be avoided?

View Answer:
Interview Response: An infinite loop is a loop that runs indefinitely without stopping. It can be avoided by ensuring that the loop's exit condition is met at some point.

Code Example:

An example of an infinite loop would be:

while (true) {
console.log('This is an infinite loop');
}

This loop will never stop because the condition for the while loop is always true.

To avoid an infinite loop, you should always ensure the loop's exit condition will be met. Here is a corrected version of the above code:

let counter = 0;
while (counter < 5) {
console.log('This will not be an infinite loop');
counter++;
}

In this corrected version, the loop will exit after it has run 5 times because counter will be equal to 5, making the condition counter < 5 false.


What is a nested loop in JavaScript, and why would you use one?

View Answer:
Interview Response: A nested loop in JavaScript refers to a loop that's located within another loop. It's commonly used to traverse and manipulate data that has a hierarchical structure, like multi-dimensional arrays.

Code Example:

Here's a simple example of a nested loop in JavaScript where we print a multiplication table.

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

This code will output:

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9

In this example, for each iteration of the outer loop (i), the inner loop (j) runs completely, performing the multiplication and logging the result. This gives us the multiplication table for numbers 1 to 3.


How do you create an infinite loop in JavaScript intentionally?

View Answer:
Interview Response: You can create an infinite loop intentionally by omitting the loop's condition or providing a condition that is always true.

Code Example:

Here's a simple example of intentionally creating an infinite loop in JavaScript:

while (true) {
console.log('This is an intentional infinite loop');
}

Remember, this code will run indefinitely and print 'This is an intentional infinite loop' until you manually stop the execution. For example, in a browser, you might need to close the tab or the entire browser.

Infinite loops should be used with caution, as they can cause your program to become unresponsive and may consume a lot of CPU resources. Always make sure there's a good reason to use them, and they are managed correctly.


note

This questions is intended to give the interviewee the chance to show whether they have a clear understanding of loop dynamics in programming.


What is the syntax for the For loop in JavaScript?

View Answer:
Interview Response: The structure of a For loop consists of three components: initialization, a condition, and an iteration, enclosed in parentheses and followed by a code block.

What is the syntax for the While loop in JavaScript?

View Answer:
Interview Response: In JavaScript, the syntax for a while loop consists of the keyword, while, followed by a condition enclosed in parentheses, and then a block of code, or loop body, enclosed in curly braces.