jQuery.each(collection, callback) (alias $.each) is a generic iterator for arrays, array-like objects, and plain objects (since jQuery 1.0). It returns the collection you passed in.
Pass a collection and a callback; optionally break with return false.
Start with an array or plain object.
Call $.each(collection, function (indexOrKey, value) { ... }).
Use index/key and value (or this) inside the callback.
Return false to stop early when needed.
Piece
Required?
Role
collection
Yes
Array, array-like, or plain object
callback
Yes
Runs once per item
Return value
—
Same collection (first argument)
Pseudocode
Pseudocode
function each(collection, callback):
if collection is array-like:
for i from 0 to length - 1:
result = callback(i, collection[i])
if result === false: break
else:
for each own key in collection:
result = callback(key, collection[key])
if result === false: break
return collection
Reference
Cheat Sheet
Keep these facts handy while iterating with jQuery.
Start at zero.total accumulates outside the callback.
Add each price inside $.each.
Format.toFixed(2) prints currency-style text.
Pattern. Same idea works for counts, filters, and side effects.
Edge Cases & Pitfalls
Check these before choosing $.each over other loops.
api
Do not confuse with $(sel).each()
DOM iteration belongs on the collection method. $.each is for any array/object you already have.
this
this boxes primitives
Prefer the value argument for numbers/strings. this may be an Object wrapper.
break
Only false breaks the loop
Returning 0, null, or undefined does not stop iteration — use explicit return false.
map
Need a new array? Prefer $.map
$.each is for side effects. Transforming values into a new list is cleaner with $.map.
Compatibility
Browser Support
jQuery.each() has shipped since jQuery 1.0 and works in all modern browsers with jQuery loaded. It is a pure iteration helper (arrays and objects), not a DOM-only API.
✓ jQuery 1.0+
jQuery.each()
Supported everywhere jQuery runs in the browser. Use it for arrays, array-likes, and plain objects.
100%Browser support
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
$.each()Excellent
Bottom line: Call $.each(collection, fn) for generic loops. Return false to break. Use $(sel).each() for DOM selections.
Analysis
Performance Notes
Approach
Cost
Best for
$.each(arr, fn)
O(n)
Arrays / objects with early exit
arr.forEach(fn)
O(n)
Arrays only (no break)
for / for…of
O(n)
Hottest loops / fine control
For typical UI sizes, $.each is fine. Prefer native loops only when profiling shows iteration is hot.
Remember
Key Takeaways
Role: generic loop for arrays and plain objects.
Break:return false stops immediately.
DOM: use $(sel).each() for selected elements.
Transform: prefer $.map when building a new array.
One line:$.each(collection, fn) walks arrays and objects — return false to break, and use collection .each() for DOM lists.
Frequently Asked Questions
jQuery.each(collection, callback) walks every item in an array, array-like object, or plain object and runs your callback once per entry. It is a generic iterator — not limited to DOM selections.
jQuery.each() is a static utility that accepts any collection. $(selector).each() only iterates elements in a jQuery DOM wrapper. Prefer $.each for arrays/objects and .each() for selected elements.
Return false from the callback to break immediately (like break in a for loop). Any other return value continues to the next item.
It returns its first argument — the same collection you passed in — so you can reuse the array or object after iterating.
Yes. this is the current value. For primitives, JavaScript boxes them as objects when accessed via this.
Use $.each when you need plain objects, array-like collections, or early exit with return false. Array.forEach only works on arrays and cannot break cleanly.