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 😊.

Leave a Comment