How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Break Out of a jQuery .each() Loop
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • 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.

    FacebookLike
    XFollow
    PinterestPin
    InstagramFollow
    Most Popular
    Set vi as the default editor in Ubuntu — a terminal opening the vim editor
    How to Set vi (Vim) as the Default Editor in Ubuntu
    June 8, 2026
    rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
    rsync Says “ALL DONE” but Files Are Missing: How to Verify
    June 8, 2026
    Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
    How to Migrate a Website to a New Server With rsync
    June 8, 2026
    Bun runtime — faster JS toolkit replacing npm in Laravel projects
    How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
    May 24, 2026
    Tailscale mesh — peer-to-peer connections between devices, coordination server
    How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
    May 24, 2026

    You Might Also Like

    Rename menu items on the WooCommerce My Account page
    Web Development

    How to Rename Menu Items on the WooCommerce My Account Page

    4 Min Read
    Include Composer packages in plain PHP projects
    Web Development

    How to Include Composer Packages in Plain PHP Projects (Autoload + Example)

    5 Min Read
    Laravel rollback specific migration — Artisan migrate:rollback --path command illustration
    Web Development

    How to Rollback a Specific Migration in Laravel

    8 Min Read
    Get a favicon URL with JavaScript
    Web Development

    How to Get a Website’s Favicon URL with JavaScript

    4 Min Read
    How7o

    We provide tips, tricks, and advice for improving websites and doing better search.

    Tools

    • Age Calculator
    • Word Counter
    • Image Upscaler
    • Password Generator
    • QR Code Generator
    • See all tools→

    Pranks

    • Fake Blue Screen Prank
    • Hacker Typer
    • Fake iMessage Generator
    • Windows XP Crash Prank
    • Windows 11 Update Prank
    • 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?