Recall & Review
beginner
What happens when a
return statement is executed inside a loop in a function?The function immediately stops running and returns the specified value. The loop and the rest of the function do not continue.
Click to reveal answer
intermediate
Can a
return statement inside a loop return different values on different iterations?Yes, if the
return depends on a condition inside the loop, it can return different values depending on which iteration triggers it first.Click to reveal answer
beginner
What is the difference between
return inside a loop and continue inside a loop?return stops the entire function and returns a value. continue skips the current loop iteration and moves to the next one without stopping the function.Click to reveal answer
beginner
Consider this code snippet:<br><pre>function findFirstEven(arr) {
for (let num of arr) {
if (num % 2 === 0) {
return num;
}
}
return null;
}</pre><br>What does this function do?It loops through the array and returns the first even number it finds. If no even number is found, it returns
null.Click to reveal answer
beginner
Is it possible for code after a
return inside a loop to run?No. Once
return runs, the function ends immediately. Any code after it inside the loop or function will not run.Click to reveal answer
What happens when a
return statement is executed inside a loop in a function?✗ Incorrect
The return statement immediately ends the function and returns the value, even if inside a loop.
If a
return is inside a loop, can the loop continue after the return?✗ Incorrect
When return runs, the whole function stops immediately, so the loop cannot continue.
What is the difference between
return and continue inside a loop?✗ Incorrect
return stops the function and returns a value. continue skips the current loop iteration and continues looping.
In this code, what will be the output?<br>
function test() {
for (let i = 0; i < 5; i++) {
if (i === 2) return i;
}
return -1;
}
console.log(test());✗ Incorrect
The function returns 2 when i === 2 and stops immediately.
If no
return is executed inside a loop, what happens after the loop ends?✗ Incorrect
If no return happens inside the loop, the function continues running code after the loop normally.
Explain what happens when a
return statement is used inside a loop in a JavaScript function.Think about how <code>return</code> affects function flow.
You got /4 concepts.
Describe a real-life example where returning inside a loop is useful in programming.
Imagine looking for something in a list and stopping once found.
You got /4 concepts.