Breaking

Breaking is a shift of control from within the current for, whileswitch, or labeled statement to the code following the statement. It effectively terminates the statement, preventing any following code from being executed.

In the context of iteration, whether or not to continue or break from iteration is usually determined by ConditionExpression within the construct itself (for example, counter < array.length), or by the length of the data structure in the case of for..in and for..of. However, it may still be necessary, at times, to break out of the iteration early.

For example, if you are looking for a specific item within a data structure (a needle-in-a-haystack situation), then it would make sense to stop looking once the item is found. We achieve that by breaking:

for (let i = 0; i < array.length; i++) {
if (myCriteriaIsMet(array[i]) {
happyPath();
break;
}
}

Breaking from an iteration will immediately halt and exit the iteration, meaning any remaining code within the containing IterationBody will not be run. The code immediately following IterationBody will then run.

The break statement is also used to break out from switch statements, typically when you have executed the relevant case statement. As we will discuss later in this chapter, the switch statement will transfer control to the case statement that is considered strictly equal (===) to the value passed to switch(...), and will then run all code following that case statement until an explicit break; (or return;yield;, or throw;) occurs:

switch (2) {
case 1: console.log(1);
case 2: console.log(2);
case 3: console.log(3);
case 4: console.log(4); break;
case 5: console.log(5);
}

// Logs: 2, 3, 4

Here, we see that a value of 2 shifts control to the matching case 2, and then all of the following code within the switch's body will run naturally until a break; statement is encountered. Hence, we only see logs for 2, 3and 4. A log for 1 is avoided as case 1 does not match the value, 2, and a log for 5 is avoided as break; occurs before it.

When case within switch does not break, it is called fallthrough. This common technique used in switch statements is useful when you want to carry out a single action or cascade of actions based on more than one matching condition (we will cover this concept more in the The switch statement se).

To the right side of the break keyword there may be a label that refers to the switchfor, or while statement. If you don't supply a label, then JavaScript will assume you are referring to the current containing iteration or switch construct. This is only useful when you have two or more breakable constructs within each other, for example, an iteration within an iteration. Observe here how we've labeled our outer for loop with the outerLoop label, enabling us to break out of it from within the inner for loop:

outerLoop: for (let obj in objects) {
for (let key in obj) {
if (/* some condition */) {
break outerLoop;
}
}
}

You can, in fact, break out of any labeled statement (even if it is outside of an iteration or switch construct) but you must explicitly provide the label:

specificWork: {
doSomeSpecificWork();
if (weAreFinished) {
break specificWork;
// immediately exits the `specificWork: {...}` block
}
doOtherWork();
}

This is very rarely applicable but is nonetheless worth knowing about in case you ever run into such code.

One last thing to note on breaking out of iterations or switch statements is that, although we typically do so by using an explicit break; statement, it is something that can also effectively occur via other mechanisms of moving control such as yielding, returning, or throwing. It's quite common, for example, to see an iteration that uses return; to break out not only of itself but also of the containing function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.79.65