0
0
Javascriptprogramming~5 mins

Return inside loops in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ANothing, the return is ignored inside loops.
BThe loop skips to the next iteration.
CThe loop finishes all iterations before returning.
DThe function stops and returns the value immediately.
If a return is inside a loop, can the loop continue after the return?
ANo, the function ends immediately.
BYes, the loop continues normally.
COnly if the return is inside an <code>if</code> statement.
DOnly in asynchronous functions.
What is the difference between return and continue inside a loop?
ABoth end the function immediately.
B<code>return</code> skips iteration; <code>continue</code> ends function.
C<code>return</code> ends the function; <code>continue</code> skips to next loop iteration.
DBoth skip to the next iteration.
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());
A0
B2
C-1
D5
If no return is executed inside a loop, what happens after the loop ends?
AThe function continues running any code after the loop.
BThe function ends automatically.
CThe loop runs again infinitely.
DThe function returns <code>undefined</code> immediately.
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.