0
0
Javascriptprogramming~10 mins

Break statement in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Break statement
Start Loop
Check Condition
Yes
Execute Loop Body
Is break condition met?
YesExit Loop
Back to Check Condition
The loop starts and checks its condition. Inside the loop body, if the break condition is met, the loop exits immediately; otherwise, it continues looping.
Execution Sample
Javascript
for (let i = 0; i < 5; i++) {
  if (i === 3) {
    break;
  }
  console.log(i);
}
This code loops from 0 to 4 but stops and exits the loop when i equals 3.
Execution Table
IterationiCondition (i < 5)Break Condition (i === 3)ActionOutput
10truefalsePrint 00
21truefalsePrint 11
32truefalsePrint 22
43truetrueBreak loop
--false-Exit loop-
💡 Loop exits at iteration 4 because break condition i === 3 is true.
Variable Tracker
VariableStartAfter 1After 2After 3Final
i01233
Key Moments - 2 Insights
Why does the loop stop printing numbers after i reaches 3?
Because at iteration 4, the break condition (i === 3) becomes true, so the break statement runs and exits the loop immediately, as shown in execution_table row 4.
Does the loop check the condition (i < 5) after break is triggered?
No, once break runs, the loop exits immediately without checking the loop condition again, as seen in execution_table where after break, the loop ends.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i when the break statement runs?
A3
B4
C2
D5
💡 Hint
Check the 'Break Condition' column in execution_table row 4 where break is triggered.
At which iteration does the loop stop executing the body?
AIteration 3
BIteration 5
CIteration 4
DIteration 1
💡 Hint
Look at the 'Action' column in execution_table where 'Break loop' happens.
If the break condition was changed to i === 5, how would the output change?
AIt would print 0 to 3
BIt would print 0 to 4
CIt would print nothing
DIt would cause an error
💡 Hint
Refer to variable_tracker and execution_table to see when break triggers and what is printed.
Concept Snapshot
Break statement in JavaScript:
- Used inside loops to exit immediately.
- Syntax: break;
- When break runs, loop stops regardless of condition.
- Useful to stop loops early based on a condition.
- After break, code continues after the loop.
Full Transcript
This visual trace shows how the break statement works in a JavaScript for loop. The loop starts with i=0 and runs while i is less than 5. Each iteration prints the value of i. When i reaches 3, the break condition becomes true, so the break statement runs and exits the loop immediately. The loop does not continue to i=4 or 5. Variables and outputs are tracked step-by-step to show how break affects the flow. This helps beginners see exactly when and why the loop stops early.