How to Use the Break Statement from a For Loop in JavaScript?

Are you still perplexed about how you are going to use the break statement from a for loop in JavaScript?

Worry no more, as we will hand you how to do that. You just have to keep reading!

To enhance your JavaScript skills when it comes to breaking and continuing statements in JavaScript.

What is break statement?

The “break” statement is a tool used to exit or terminate a loop immediately when a certain condition is met.

Rather than waiting for the loop to complete naturally.

Imagine you’re in a loop and you need to exit it abruptly for some reason, that’s where the “break” statement comes into play.

As soon as the program encounters a “break” statement within a loop, it immediately jumps out of the loop and continues with the code right after the loop.

It’s like a quick way to escape from the loop and continue with the rest of the program.

Syntax

break ;

or

✅ break labelname;

Important note: The labelname is optional, and it is rarely used. It only happens when you use the break statement outside a loop or switch. You must include the labelname identifier.

How to use the Break Statement in a For Loop in JavaScript?

The break statement is used to exit a loop early, stopping the execution of the remaining code inside the loop.

It can be used in a for and while loop and labeled blocks in JavaScript to break out of the loop when a certain condition is met.

Here are examples of using the break statement in a for loop, a while loop, and a labeled block in JavaScript:

Example 1: Using break statement in for Loop

for (let i = 0; i < 5; i++) {
if (i === 4) {
break;
}
console.log(i);
}

Output:

0
1
2
3

Example 2: Using break statement in while Loop

let i = 0;
while (i < 5) {
if (i === 5) {
break;
}
console.log("i);
i++;
}

Output:

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4

Here’s another example:

let i = 0;
while (true) {
    console.log(i);
    i += 1;
    if (i > 5) {
        break;
    }
}

Output:

0
1
2
3
4
5

Example 3: Using break statement in labeled block

let numbers = [1, 2, 3, 4, 5];
let letters = ['a', 'b', 'c', 'd', 'e'];
let result = '';

outerLoop: for (let i = 0; i < numbers.length; i++) {
    innerLoop: for (let j = 0; j < letters.length; j++) {
        if (numbers[i] === 5 && letters[j] === 'e') {
            break outerLoop;
        }
        result += numbers[i] + letters[j] + ' ';
    }
}

console.log(result);

Output:

1a 1b 1c 1d 1e 2a 2b 2c 2d 2e 3a 3b 3c 3d 3e 4a 4b 4c 4d 4e 5a 5b 5c 5d 

What is the difference between break and continue statement in JavaScript?

The break and continue statements are used to control the flow of execution in a loop in JavaScript.

The main difference between the two is that the break statement is used to immediately terminate a loop, while the continue statement is used to skip the current iteration of a loop and move on to the next iteration.

For example, consider a for loop that iterates over an array of numbers. If you want to exit the loop as soon as a certain condition is met, you can use the break statement.

On the other hand, if you want to skip over certain elements in the array based on a condition, you can use the continue statement.

Here’s an example that demonstrates the difference between break and continue:

Using break statements

let numbers = [10, 20, 30, 40, 50];

// Using break
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === 40) {
break;
}
console.log(numbers[i]);
}

Output:

10
20
30

Using continue statements

let numbers = [10, 20, 30, 40, 50];

// Using continue
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === 40) {
continue;
}
console.log(numbers[i]);
}

Output:

10
20
30
50


In the first loop, we use the break statement to exit the loop as soon as we encounter the number 40.

In the second loop, we use the continue statement to skip over the number 40 and continue iterating over the rest of the elements in the array.

Conclusion

In conclusion, the article discusses the concept of the “break” statement in JavaScript, which is used to immediately exit or terminate a loop when a certain condition is met.

This article provides examples of using the “break” statement in for loops, while loops, and labeled blocks. Also, highlights the difference between the “break” and “continue” statements, where “break” terminates the loop, while “continue” skips the current iteration and moves to the next one.

By understanding of using the “break” statement we can guarantee that you’ll have better control flow in JavaScript loops.

We are hoping that this article provides you with enough information that helps you understand the break from loop JavaScript.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Leave a Comment