To break out of a jQuery .each() loop, return false from the callback. The plain break statement doesn’t work inside $.each because the loop body is a callback function — break has no enclosing loop to escape from. Return false instead, and jQuery stops iterating immediately.
Last verified: 2026-05-17 with jQuery 3.7+. Originally published 2023-01-19, rewritten and updated 2026-05-17.
TL;DR
// BREAK — return false stops the loop
$('selector').each(function () {
if (condition) {
return false;
}
});
// CONTINUE — plain return skips to next iteration
$('selector').each(function () {
if (skipThisOne) {
return;
}
// ...
});
Why break doesn’t work
JavaScript’s break exits the directly-surrounding for, while, or switch block. Inside $.each, the loop body is a function — there’s no loop right above the break:
// BAD — throws "Illegal break statement"
$('li').each(function () {
if ($(this).hasClass('stop')) {
break; // ← no enclosing loop here
}
});
The fix is jQuery’s documented contract: return false from the callback. $.each checks the return value after each iteration and stops the loop if you returned a literal false.

Working example
// Find the first with data-stop="true" and stop iterating
$('li').each(function (i, el) {
if ($(el).data('stop')) {
console.log('Stopping at index ' + i);
return false;
}
console.log('Visiting ' + i);
});
Skip to next (continue)
$('li').each(function () {
if ($(this).hasClass('hidden')) {
return; // skip this one, keep iterating
}
// ... process visible items only
});
A plain return (or return true) advances to the next iteration. Only return false stops the whole loop.
Native Array.forEach: can’t be stopped
Array.prototype.forEach doesn’t have a break mechanism — return values are ignored. If you need short-circuit behavior on a plain array (no jQuery), use one of these instead:
// stops when callback returns true
[1, 2, 3, 4].some(n => { if (n === 2) return true; });
// stops when callback returns false
[1, 2, 3, 4].every(n => n < 3);
// for...of with break
for (const n of [1, 2, 3, 4]) {
if (n === 2) break;
}
Frequently asked questions
break fail in $.each()? break is a statement that exits the surrounding for, while, or switch. Inside $.each(), the body of the loop runs as a callback function — and break only operates on the directly-enclosing control-flow block, not the function that called you. The browser throws Illegal break statement (or silently misbehaves in older engines) because there’s no loop directly above break.
return false actually do? jQuery’s $.each checks the return value of your callback after each iteration. If you return false explicitly, the loop stops immediately and no further iterations run. Any other return value (including undefined from a plain return) means ‘keep going.’ This is jQuery’s documented contract — see api.jquery.com/jquery.each.
continue? A plain return (no value, or any non-false value). It exits the current iteration and lets $.each move to the next item — exactly like continue in a regular loop. return true works too; return false is the one specific value that stops the whole loop.
Array.prototype.forEach? No. Native forEach can’t be stopped — returning false just continues to the next item. To break out of a native loop, use a regular for / for...of, or some() / every() / find() which short-circuit when the callback returns true/false.
return false from inside a nested function? No — the return exits whichever function it sits directly inside. If the loop callback calls another helper function and you return false from the helper, the loop continues. Hoist the condition back up into the $.each callback itself, or use a flag variable that the outer callback inspects.
Related guides
- How to Add the Required Attribute to Input Fields with jQuery
- How to Auto-focus a Select2 Dropdown on Page Load
References
jQuery $.each reference: api.jquery.com/jquery.each.