In this tutorial, we'll learn about two essential keywords in JavaScript that help control loop execution: break
and continue
. These keywords provide the flexibility to jump out of loops or skip iterations, making your code more efficient and readable.
By the end of the tutorial, you should be able to:
- Understand the purpose and usage of the break
and continue
statements
- Implement these statements in your JavaScript code
Before you start, you should be familiar with the basics of JavaScript, including variables, data types, and loops.
break
statementThe break
statement is used to exit from a loop early, stopping its execution completely. As soon as the JavaScript engine encounters a break
statement, it jumps out of the current loop and continues executing the code that follows.
Here's a simple example:
for (let i = 0; i < 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}
In this code, the loop will only print numbers from 0 to 4. As soon as i
equals 5, the break
statement is executed, and the loop is terminated.
continue
statementThe continue
statement, on the other hand, doesn't terminate the loop. Instead, it skips the current iteration and jumps to the next one.
Here's how you can use the continue
statement:
for (let i = 0; i < 10; i++) {
if (i === 5) {
continue;
}
console.log(i);
}
In this example, the loop will print numbers from 0 to 9, but not 5. Once i
equals 5, the continue
statement is executed, the current iteration is skipped, and the loop continues with the next iteration.
Let's look at some practical examples to understand these concepts better.
break
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > 5) {
break;
}
console.log(numbers[i]);
}
The output will be: 1 2 3 4 5
In this example, as soon as a number greater than 5 is encountered, the loop is terminated, and the program stops printing numbers.
continue
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === 5) {
continue;
}
console.log(numbers[i]);
}
The output will be: 1 2 3 4 6 7 8 9 10
In this case, when the number 5 is encountered, the continue
statement skips the current iteration, and the number 5 is not printed.
In this tutorial, we learned how to use the break
and continue
statements in JavaScript. The break
statement allows us to exit a loop prematurely, while the continue
statement lets us skip an iteration. Both are powerful tools for controlling loop execution and can lead to more efficient and readable code.
For further study, consider exploring nested loops and how break
and continue
work within them. For additional resources, the Mozilla Developer Network provides comprehensive documentation on JavaScript.
["Apple", "Banana", "Cherry", "Date", "Elderberry"]
and stops as soon as it encounters a fruit starting with 'D'.For each exercise, try to solve it first, then check the solutions below.
for (let i = 1; i <= 10; i++) {
if (i % 7 === 0) {
break;
}
console.log(i);
}
for (let i = 1; i <= 20; i++) {
if (i % 3 === 0 || i % 5 === 0) {
continue;
}
console.log(i);
}
let fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry"];
for (let i = 0; i < fruits.length; i++) {
if (fruits[i].startsWith('D')) {
break;
}
console.log(fruits[i]);
}