Bird
Raised Fist0
Javascriptprogramming~10 mins

Break statement in Javascript - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to stop the loop when i equals 3.

Javascript
for (let i = 0; i < 5; i++) {
  if (i === 3) {
    [1];
  }
  console.log(i);
}
Drag options to blanks, or click blank then click option'
Abreak
Breturn
Ccontinue
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'continue' instead of 'break' which skips the current iteration but does not stop the loop.
Using 'return' inside a loop without a function context causes errors.
2fill in blank
medium

Complete the code to exit the while loop when count reaches 5.

Javascript
let count = 0;
while (true) {
  if (count === 5) {
    [1];
  }
  console.log(count);
  count++;
}
Drag options to blanks, or click blank then click option'
Acontinue
Bbreak
Cexit
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'continue' which skips the rest of the loop but does not stop it.
Using 'exit' or 'stop' which are not valid JavaScript keywords.
3fill in blank
hard

Fix the error in the code to stop the loop when num is 10.

Javascript
for (let num = 0; num < 20; num++) {
  if (num === 10) {
    [1];
  }
  console.log(num);
}
Drag options to blanks, or click blank then click option'
Acontinue
Bexit
Cbreak
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'continue' which skips the current iteration but does not stop the loop.
Using invalid keywords like 'exit' or 'stop'.
4fill in blank
hard

Fill both blanks to stop the loop when the letter is 'c'.

Javascript
const letters = ['a', 'b', 'c', 'd'];
for (let i = 0; i < letters.length; i++) {
  if (letters[i] [1] 'c') {
    [2];
  }
  console.log(letters[i]);
}
Drag options to blanks, or click blank then click option'
A===
B!==
Cbreak
Dcontinue
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' or '!==' instead of '===' for equality check.
Using 'continue' which skips iteration but does not stop the loop.
5fill in blank
hard

Fill all three blanks to stop the loop when the number is greater than 7.

Javascript
const numbers = [3, 5, 8, 10];
for (const num [1] numbers) {
  if (num [2] 7) {
    [3];
  }
  console.log(num);
}
Drag options to blanks, or click blank then click option'
Aof
B>
Cbreak
Din
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in' instead of 'of' to loop over array values.
Using '<' instead of '>' in the condition.
Using 'continue' instead of 'break' to stop the loop.

Practice

(1/5)
1. What does the break statement do inside a loop in JavaScript?
easy
A. Pauses the loop for a moment
B. Stops the loop immediately and exits it
C. Restarts the loop from the beginning
D. Skips the current iteration and continues with the next

Solution

  1. Step 1: Understand the purpose of break

    The break statement is used to stop a loop or switch case immediately.
  2. Step 2: Compare with other loop controls

    Unlike continue which skips to the next iteration, break exits the loop fully.
  3. Final Answer:

    Stops the loop immediately and exits it -> Option B
  4. Quick Check:

    break stops loop = D [OK]
Hint: Break exits loop immediately, unlike continue which skips iteration [OK]
Common Mistakes:
  • Confusing break with continue
  • Thinking break pauses the loop
  • Assuming break restarts the loop
2. Which of the following is the correct syntax to use break inside a for loop?
easy
A. for(let i=0; i<5; i++) { if(i===3) break; }
B. for(let i=0; i<5; i++) { break if(i===3); }
C. for(let i=0; i<5; i++) { if break(i===3); }
D. for(let i=0; i<5; i++) { if(i===3) continue break; }

Solution

  1. Step 1: Check the correct placement of break

    The break statement must be inside a block and triggered by a condition like if(i===3).
  2. Step 2: Validate syntax correctness

    for(let i=0; i<5; i++) { if(i===3) break; } uses correct JavaScript syntax: if(condition) break;. Other options misuse break syntax.
  3. Final Answer:

    for(let i=0; i<5; i++) { if(i===3) break; } -> Option A
  4. Quick Check:

    Correct break syntax = B [OK]
Hint: Use break inside if block: if(condition) break; [OK]
Common Mistakes:
  • Placing break before if
  • Using break as a function
  • Combining continue and break incorrectly
3. What will be the output of this code?
let sum = 0;
for(let i = 1; i <= 5; i++) {
  if(i === 3) break;
  sum += i;
}
console.log(sum);
medium
A. 3
B. 6
C. 10
D. 15

Solution

  1. Step 1: Trace the loop iterations

    The loop runs from i=1 to i=5, but breaks when i equals 3, so it runs for i=1 and i=2 only.
  2. Step 2: Calculate the sum before break

    sum = 0 + 1 + 2 = 3; loop breaks before adding 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Sum before break at i=3 = 3 [OK]
Hint: Add only before break triggers; break stops loop early [OK]
Common Mistakes:
  • Adding value after break condition
  • Assuming loop runs fully
  • Confusing break with continue
4. Find the error in this code snippet:
for(let i=0; i<5; i++) {
  if(i === 2)
    break
  console.log(i);
}
medium
A. Missing semicolon after break causes syntax error
B. break cannot be used inside for loops
C. No error, code runs correctly
D. console.log should be before break

Solution

  1. Step 1: Check syntax of break statement

    In JavaScript, semicolon after break is optional if line ends there, so no syntax error.
  2. Step 2: Verify loop and break usage

    break is valid inside for loops and console.log after break is fine; code stops when i=2.
  3. Final Answer:

    No error, code runs correctly -> Option C
  4. Quick Check:

    break syntax valid without semicolon = C [OK]
Hint: Semicolon after break is optional if line ends there [OK]
Common Mistakes:
  • Thinking semicolon is mandatory after break
  • Believing break can't be in loops
  • Assuming console.log must be before break
5. You want to find the first number divisible by 7 in an array and stop checking after finding it. Which code correctly uses break to do this?
const numbers = [10, 13, 21, 28, 35];
let result = null;
for(let num of numbers) {
  if( /* condition here */ ) {
    result = num;
    break;
  }
}
console.log(result);
hard
A. if(num === 7)
B. if(num / 7 === 0)
C. if(num % 7)
D. if(num % 7 === 0)

Solution

  1. Step 1: Identify condition for divisibility by 7

    Use modulo operator: num % 7 === 0 checks if remainder is zero, meaning divisible.
  2. Step 2: Confirm break usage stops loop after first match

    When condition is true, assign result and break to stop checking further.
  3. Final Answer:

    if(num % 7 === 0) -> Option D
  4. Quick Check:

    Modulo zero means divisible = A [OK]
Hint: Use modulo (%) to check divisibility, then break [OK]
Common Mistakes:
  • Using division instead of modulo
  • Checking if num equals 7 only
  • Using if(num % 7) which is truthy for non-zero
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard