How7oHow7o
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Break Out of a jQuery .each() Loop
Share
Notification Show More
Font ResizerAa
How7oHow7o
Font ResizerAa
  • Tools
  • Prank Screens
  • Learn
  • Blog
Search
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Break Out of a jQuery .each() Loop
Web Development

How to Break Out of a jQuery .each() Loop

how7o
By how7o
Last updated: May 22, 2026
5 Min Read
Break out of a jQuery .each() loop with return false
SHARE

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.

Contents
  • TL;DR
  • Why break doesn’t work
  • Working example
  • Skip to next (continue)
  • Native Array.forEach: can’t be stopped
  • Frequently asked questions
  • Related guides
  • References

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.

Break out of jQuery .each() — return false stops, plain return skips to next

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

    Why does 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.

    What does 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.

    What’s the equivalent of 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.

    Does the same trick work in 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.

    Can I use 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.

    TAGGED:JavaScriptjQuery

    Sign Up For Daily Newsletter

    Be keep up! Get the latest breaking news delivered straight to your inbox.
    [mc4wp_form]
    By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
    Share This Article
    Facebook Copy Link Print
    Previous Article XAMPP autostart Apache and MySQL on Windows — XAMPP Control Panel and services How to Auto-start Apache and MySQL with XAMPP on Windows
    Next Article cd to a different drive in Windows — D: vs cd /d D:path How to cd to a Different Drive in Windows
    Leave a Comment

    Leave a Reply Cancel reply

    You must be logged in to post a comment.

    You Might Also Like

    Extract a .tar.gz archive in PHP with PharData
    Web Development

    How to Extract a .tar.gz Archive in PHP

    5 Min Read
    Laravel leftJoin to keep all records even without matches
    Web Development

    How to Use LEFT JOIN in Laravel to Keep All Records

    4 Min Read
    WordPress order posts by meta value — WP_Query or pre_get_posts with meta_value_num
    Web Development

    How to Order Posts by Meta Value in WordPress

    8 Min Read
    Install PHP on Ubuntu — terminal with apt install php command and stylized elephant icon
    Web Development

    How to Install PHP on Ubuntu (22.04 & 24.04): Step-by-Step Guide

    9 Min Read
    How7oHow7o

    Free browser-based tools, prank screens, and step-by-step tech tutorials. No signup, nothing uploaded.

    • Private by design — your files never leave your device
    • Instant — no signup, no install, no waiting
    • Always free — no trials, no watermarks, no paywalls

    Tools

    • PDF Editor
    • Image Upscaler
    • JPEG Compressor
    • Password Generator
    • QR Code Generator
    • Video Trimmer
    • See all tools →

    Pranks

    • Cracked Screen Prank
    • Fake Blue Screen (BSOD)
    • Fake Disk Format
    • Fake Low Battery
    • Fake Nintendo Switch Update Screen
    • Fake PlayStation Update Screen
    • See all prank screens →

    Company

    • About Us
    • Blog
    • Contact
    • Privacy Policy
    • Terms of Service
    • Sitemap
    © 2024–2026 How7o. All rights reserved.
    Welcome Back!

    Sign in to your account

    Username or Email Address
    Password

    Lost your password?