Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Breaking Out of Loops with break | Looping Through Data in JavaScript
Introduction to JavaScript

bookBreaking Out of Loops with break

The break keyword is a loop terminator, allowing you to exit a loop prematurely.

Example 1: Stopping an Infinite Loop

123456789
let i = 1; while (true) { console.log("Iteration", i); i++; if (i > 5) { break; } }
copy

In this example, the break keyword stops the infinite while loop when i becomes equal to 6, after the i++ operation.

Example 2: Breaking a for Loop

123456789
let a = 0; for (let i = 0; i < 10; i++) { a += i; console.log("a =", a); if (i > 3) { break; } }
copy

Here, the variable a is incremented by i during each iteration (0 + 1 + 2 + 3 + 4) until i becomes equal to 4. At that point, the break statement is triggered.

i = 0a = 0 + 0
i = 1a = 0 + 1
i = 2a = 1 + 2
i = 3a = 3 + 3
i = 4a = 6 + 4, break

Example 3: Breaking a while Loop Immediately

1234567
while (true) { console.log("Iteration start"); break; console.log("Iteration end"); // This line will not be executed } console.log("Loop was ended");
copy

In this example, the break statement inside the while loop immediately terminates the loop, preventing any code below it from executing within the same block.

Note

Remember that the break statement is a powerful tool for controlling the flow of your loops, allowing you to exit them when specific conditions are met.

question mark

What will be printed to the console by the following code?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookBreaking Out of Loops with break

The break keyword is a loop terminator, allowing you to exit a loop prematurely.

Example 1: Stopping an Infinite Loop

123456789
let i = 1; while (true) { console.log("Iteration", i); i++; if (i > 5) { break; } }
copy

In this example, the break keyword stops the infinite while loop when i becomes equal to 6, after the i++ operation.

Example 2: Breaking a for Loop

123456789
let a = 0; for (let i = 0; i < 10; i++) { a += i; console.log("a =", a); if (i > 3) { break; } }
copy

Here, the variable a is incremented by i during each iteration (0 + 1 + 2 + 3 + 4) until i becomes equal to 4. At that point, the break statement is triggered.

i = 0a = 0 + 0
i = 1a = 0 + 1
i = 2a = 1 + 2
i = 3a = 3 + 3
i = 4a = 6 + 4, break

Example 3: Breaking a while Loop Immediately

1234567
while (true) { console.log("Iteration start"); break; console.log("Iteration end"); // This line will not be executed } console.log("Loop was ended");
copy

In this example, the break statement inside the while loop immediately terminates the loop, preventing any code below it from executing within the same block.

Note

Remember that the break statement is a powerful tool for controlling the flow of your loops, allowing you to exit them when specific conditions are met.

question mark

What will be printed to the console by the following code?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 6
some-alt