EN
JavaScript - break statement
0 points
In this article, we would like to show you break statement in JavaScript.
xxxxxxxxxx
1
break [label];
The break statement ends the current loop, switch, or label statement and proceeds to the statement following the ended statement.
In this example, we use break
statement to terminate the while
loop on the specific condition.
xxxxxxxxxx
1
let i = 0;
2
3
while (i < 5) {
4
if (i === 2) {
5
break;
6
}
7
i = i + 1;
8
}
9
10
console.log(i); // 2
Note:
If the statement we want to break is not a loop or switch,
label
is required.
In this example, we use the break
statement to terminate the switch
statement when a case is matched and the corresponding code has been executed.
xxxxxxxxxx
1
const number = 2;
2
3
switch (number) {
4
case 1:
5
console.log('case 1 message...');
6
break;
7
case 2:
8
console.log('case 2 message...');
9
break;
10
default:
11
console.log('default message...');
12
break;
13
}