JavaScriptFundamental (ES6), part 1
Labeled break statements in JavaScript
85 A label, and the outer loop a bare break cannot leave
easySearch a grid of Lagos areas for one name and stop the moment you find it — both loops, not just the inner one. Then use continue on a label to abandon the rest of a row, put a label on a plain block, and show which labelled statements the parser refuses.
Stuck? Three hints, smallest first
- 1. A label is a name followed by a colon written in front of a statement:
outer: for (...) { ... }. - 2.
break outerleaves the statement the label names.continue outerjumps to that loop’s next iteration instead. - 3. A label on a plain
{ }block works too, but only withbreak— there is nothing to continue.
const zones = [
['Ikeja', 'Agege', 'Ojota'],
['Yaba', 'Surulere', 'Lekki'],
['Ajah', 'Ikorodu', 'Epe'],
];
let visits = 0;
const findCell = (grid, wanted) => {
let found = null;
outer: for (let r = 0; r < grid.length; r++) {
for (let c = 0; c < grid[r].length; c++) {
visits++;
if (grid[r][c] === wanted) {
found = [r, c];
break outer; // leaves BOTH loops
}
}
}
return found;
};
const cell = findCell(zones, 'Surulere');
const cellVisits = visits;
const cellsInGrid = zones.flat().length;
const innerOnly = [];
for (const row of zones) {
for (const area of row) {
if (area.startsWith('A')) break; // only the inner loop; the outer one carries on
innerOnly.push(area);
}
}
const clean = [];
rows: for (const row of zones) {
for (const area of row) {
if (area === 'Lekki') continue rows; // give up on this row entirely
}
clean.push(row[0]);
}
const viaReturn = (grid, wanted) => {
for (let r = 0; r < grid.length; r++) {
for (let c = 0; c < grid[r].length; c++) {
if (grid[r][c] === wanted) return [r, c];
}
}
return null;
};
const returned = [viaReturn(zones, 'Surulere'), viaReturn(zones, 'Abuja')];
const band = (kobo) => {
let label = 'none';
check: {
if (kobo <= 0) break check;
label = 'small';
if (kobo < 100000) break check;
label = 'large';
}
return label;
};
const bands = [band(0), band(50000), band(500000)];
const parse = (src) => {
try { new Function(src); return 'compiled'; } catch (error) { return error.constructor.name; }
};
const refused = [parse('check: { continue check; }'), parse('outer: for (const x of [1]) { break nope; }')];
console.log('found :', cell, '| cells visited:', cellVisits, 'of', cellsInGrid);
console.log('bare break:', innerOnly);
console.log('continue :', clean, '| by return:', returned);
console.log('block :', bands, '| refused:', refused);
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('break with a label leaves both loops at once', cell, [1, 1]);
test('and it really stops the work, five cells of nine', [cellVisits, cellsInGrid], [5, 9]);
test('a bare break leaves the inner loop only, so the outer one keeps going', innerOnly, ['Ikeja', 'Yaba', 'Surulere', 'Lekki']);
test('continue on a label abandons the rest of that outer iteration', clean, ['Ikeja', 'Ajah']);
test('a small function with return needs no label at all', returned, [[1, 1], null]);
test('a label on a plain block gives a one-way exit out of it', bands, ['none', 'small', 'large']);
test('continue only names a loop, and an unknown label never compiles', refused, ['SyntaxError', 'SyntaxError']);
test('a label is not a binding, so nothing called rows exists', typeof rows, 'undefined');
A label names a statement so break and continue can say which one they mean. Without it they take the nearest enclosing loop, which is why the second block above keeps iterating the outer array after the inner break: it left one loop, not the search. The usual workarounds — a found flag tested in the outer condition, or a thrown exception used as a jump — are both worse than one word in front of the for.
continue label is the less familiar half and the more useful one. It does not leave the labelled loop, it moves it on, which reads as "this row is disqualified, next row" with no nesting of conditions. Put the label on a plain block and you get a forward-only escape, handy for a short validation ladder where each step may finish the job early.
Two limits worth knowing. Labels are not variables: they live in their own namespace, cannot be stored or passed, and are invisible to typeof. And the parser checks them, so continue on a block label or break on a name that is not in scope is a SyntaxError at compile time rather than a runtime failure — which is why both attempts above had to be handed to new Function to be caught at all. When the loop body grows past a screen, extracting it into a function and returning is usually clearer than either.