The jQuery.each() utility walks arrays, array-like objects, and plain objects with a single callback pattern. This tutorial covers syntax, five worked examples, how return false breaks the loop, and how $.each differs from DOM .each() and native Array.forEach.
01
Syntax
$.each(col, fn)
02
Arrays
index, value
03
Objects
key, value
04
Break
return false
05
Return
Same collection
06
this
Current value
Fundamentals
Introduction
Looping over data is one of the most common tasks in JavaScript — whether you have a list of numbers, a configuration object, or an array-like NodeList. jQuery provides a generic iterator called jQuery.each() (also written $.each()) that handles all of these with one consistent API.
Unlike $(selector).each(), which only walks elements in a jQuery DOM collection, the utility form accepts any array or object as its first argument. That makes it ideal for data processing, object inspection, and plugin internals — anywhere you need a portable loop without binding to the DOM.
Concept
Understanding the jQuery.each() Method
jQuery.each(collection, callback) invokes your callback once for every enumerable property. For arrays and array-like objects, the callback receives (index, value) where index runs from 0 to length - 1. For plain objects, it receives (key, value) for each own enumerable property.
Returning false from the callback stops the loop immediately — the jQuery equivalent of break. Any other return value (including undefined) continues to the next item, like continue. The method always returns the original collection (the first argument), and inside the callback this refers to the current value.
💡
Beginner Tip
Think of $.each as a universal for-loop helper. Pass any collection, write one callback, and jQuery handles the indexing — whether your data is an array, an object, or something array-like.
On plain objects, the first callback argument is the property name (key), not a numeric index. This is why $.each is handy for config maps and JSON-like data.
📈 Practical Patterns
Early exit, aggregation, and DOM updates.
Example 3 — Break Early with return false
Stop iterating as soon as a condition is met — returning false breaks the loop like break in a for loop.
jQuery
$.each(["a", "b", "c", "d"], function (i, letter) {
console.log(letter);
if (letter === "c") {
return false; // stop — "d" is never logged
}
});
Only false breaks the loop. Returning true, a number, or nothing at all continues to the next item — unlike a regular function where any return exits early.
Example 4 — Sum Array Values
Accumulate a running total while iterating — a common real-world use of $.each in data processing.
jQuery
var prices = [9.99, 14.50, 3.25];
var total = 0;
$.each(prices, function (i, price) {
total += price;
});
console.log("Total: " + total.toFixed(2));
Declare an accumulator outside the loop, update it inside the callback. $.each returns the original prices array unchanged — your callback owns any mutations.
Example 5 — Words and Numerals DOM Demo
Official jQuery documentation style: update page elements from both an array and an object, with early exit on the array pass.
jQuery
var arr = ["one", "two", "three", "four", "five"];
var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };
$.each(arr, function (i, val) {
$("#" + val).text("Mine is " + val + ".");
return val !== "three"; // stop after "three"
});
$.each(obj, function (i, val) {
$("#" + i).append(document.createTextNode(" — " + val));
});
<div id="one">Mine is one. — 1</div>
<div id="two">Mine is two. — 2</div>
<div id="three">Mine is three. — 3</div>
<div id="four"></div> (unchanged — array loop stopped early)
<div id="five"></div> (unchanged)
How It Works
The array pass sets text on the first three divs then returns false when val === "three". The object pass appends numerals to whichever divs were updated — demonstrating both collection types in one page.
Applications
🚀 Use Cases
Config inspection — walk settings objects and apply defaults or validate keys.
Data aggregation — sum, filter, or transform arrays without manual index management.
Plugin internals — jQuery itself uses $.each throughout its source for portable loops.
Early exit searches — find the first matching item and return false to stop.
🧠 How jQuery.each() Processes a Collection
1
Detect type
jQuery checks whether the collection is array-like (has a numeric length) or a plain object.
Inspect
2
Invoke callback
For each item, call your function with (index, value) or (key, value). Set this to the current value.
Iterate
3
Check return
If the callback returns false, stop immediately. Any other value continues to the next item.
Control
=
🔗
Return collection
The original collection reference is returned — ready to reuse or chain.
Important
📝 Notes
jQuery.each() is not the same as $(selector).each() — different methods, different first arguments.
Only return false breaks the loop; returning true or any other value continues.
Inside the callback, this equals the current value (primitives are wrapped as Object).
The method returns the first argument — the collection itself — not a new array or object.
Available since jQuery 1.0 — one of the oldest and most stable jQuery APIs.
Compatibility
Browser Support
jQuery.each() has been part of jQuery since jQuery 1.0+. It works wherever jQuery runs — all modern browsers and legacy IE when paired with a supported jQuery build.
✓ jQuery 1.0+
jQuery jQuery.each()
Supported in jQuery 1.x, 2.x, and 3.x. Behavior is consistent across browsers because jQuery owns the iteration implementation.
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
jQuery.each()Universal
Bottom line: Safe in any project that already includes jQuery. One of the most battle-tested utilities in the library.
Wrap Up
🎉 Conclusion
The jQuery.each() utility is jQuery’s universal iterator for arrays, array-like objects, and plain objects. Pass any collection, write one callback, and use return false when you need to break early.
Practice the five examples above, then explore the Utilities hub and jQuery introduction for how $.each fits into the broader jQuery ecosystem.
Use $.each for plain objects and array-like collections
Return false to break; omit return to continue
Prefer $(sel).each() when iterating DOM elements
Use callback parameters (index, value) for clarity
Remember $.each returns the original collection
❌ Don’t
Confuse $.each with $(sel).each()
Expect return true to break the loop
Mutate object keys while iterating the same object
Reach for $.each when Array.forEach suffices
Assume iteration order on objects is guaranteed across engines
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about jQuery.each()
Iterate any collection the jQuery way.
5
Core concepts
↻01
$.each
Static utility
API
[ ]02
Arrays
index, value
Type
{ }03
Objects
key, value
Type
✖04
false
Break loop
Control
↩05
Return
Same collection
Result
❓ Frequently Asked Questions
jQuery.each(collection, callback) walks every item in an array, array-like object, or plain JavaScript object and invokes your callback once per entry. It is a generic iterator — not tied to DOM selections.
jQuery.each() is a static utility that accepts any collection as its first argument. $(selector).each() only iterates over elements inside a jQuery DOM wrapper. The callback signatures differ: $.each uses (index, value) for arrays and (key, value) for objects, while DOM .each() passes (index, element).
Return false from the callback function. That breaks the loop immediately — like break in a for loop. Returning any other value (including undefined) continues to the next item, similar to continue.
It returns the first argument — the same collection reference you passed in. That lets you iterate and still reuse or chain with the original array or object.
Yes. Inside the callback, this refers to the current value being iterated. For primitive values, JavaScript wraps them as Object instances so this works consistently.
Use jQuery.each() when you need to iterate plain objects, array-like objects (such as NodeLists), or when you want return false to break early. Array.forEach() only works on arrays and cannot be stopped mid-loop without throwing.
Did you know?
jQuery uses $.each internally throughout its own source code — the same utility you call in your scripts. When you iterate a jQuery object with $(sel).each(), jQuery ultimately delegates to this core iterator under the hood.