Continuing

Continuing is a shift of control from the current statement to the potential start of the next iteration. It is achieved via a continue statement.

The continue statement is valid in all iteration constructs, including for, while, do...whilefor...in, and for...of.

Here is an example of continuing conditionally, so that the body of the iteration does not execute for a specific item but the iteration still continues to progress:

const numbers = [1, 2, 3];

for (const n of numbers) {
if (n === 2) continue;
console.log(n);
}

// Logs: 1, 3

Continuing skips all of the code following continue in the current iteration and then moves onto whatever would naturally occur next. 

Similar to the break statement, to the right side of the continue keyword can optionally be a label that indicates which iteration construct should be continued. If you don't supply it, then JavaScript will assume you are referring to the current iteration construct. If you have two or more iteration constructs nested within each other, then it may be necessary to use an explicit label:

objectsIteration: for (let obj in objects) {
for (let key in obj) {
if (/* some condition */) {
continue objectsIteration;
}
}
}
The continue statement will only work in our native looping constructs. If we wish to continue in an abstracted looping construct such as Array#forEach, then we'll typically want to use a return statement instead (to return from the callback and hence continue the iteration).

Since continuing is a movement of control, we want to remain cautious about how clearly we are communicating our intent. If we have several layers of loops or several continue or break statements, it can burden the reader with an unnecessary level of complexity. 

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

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