javaScript break statement examples

The break statement in javaScript can be used to break out of a loop. It can also be used in combination with labels to break a specific loop from within two or more nested loops. There are other ways to break a loop as well, such as using the return keyword within the body of a function for example, and there is also the continue keyword as well that can be used to skip a body of code and continue a loop as well. In this post however I will be focusing on the break statement and some basic use case examples as to why it might come in handy now and then.

1 - javaScript break

The break keyword can be used to break out of a loop when a certain condition is met. This can help avoid having to loop threw the full contents of an array for example.

1
2
3
4
5
6
let arr = [3, 'foo', 4],
i = arr.length;
while (i--) {
if (typeof arr[i] === 'string') break;
}
console.log(i); // 1

With a simple example like this it does not make much of any difference really, but then it comes to a far more complex block of code that involves a much larger array, and some resource intensive code that does not need to be applied for all elements in an array, or all permutations of a loop the break keyword can be used in conjunction with if statements and continue to help reduce the number of operations preformed.

2 - switch statements

I do not come across switch statements often, and I do my best to avoid using them but never the less when working with a switch statment the break keyword is also used for each case of a switch.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
let foo = (bar) => {
switch (bar) {
case 'foo':
return 'foobar';
break;
case 'answer':
return 42;
break;
}
return 'bar';
}
console.log(foo()); // 'bar'
console.log(foo('foo')); // 'foobar'
console.log(foo('answer')); // 42

3 - break and labels

When making something that has two or more nested loops it is possible to use labels as a way to break out of the desired loop within the nested loops. By default the break keyword will break out of the loop from which it is called, but if for some reason I want to break out of a loop that is one other than that one I can use labels as a way to go about doing just that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
let findGuy = (grid) => {
let y = 0,
x,
c = 0,
cell = {};
outer: while (y < grid.h) {
x = 0;
while (x < grid.w) {
cell = grid.cells[y * grid.w + x];
if (cell.guy) {
break outer;
}
c += 1;
x += 1;
}
y += 1;
}
if (cell.hp > 0) {
return {
guy: cell,
c: c
};
}
return {
guy: false,
c: c
};
};
console.log(findGuy({
h: 3,
w: 3,
cells: [0, 0, 0, 0, 0, 0, 0, 0, 0]
})); // {guy: false, c:9}
console.log(findGuy({
h: 3,
w: 3,
cells: [0, 0, {guy:true,hp:10}, 0, 0, 0, 0, 0, 0]
})); // { guy: { guy: true, hp: 10 }, c: 2 }