jQuery.each() Method

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is jQuery.each()?

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.

Remember
$.each(array, function(index, value) { ... })
$.each(object, function(key, value) { ... })

return false  → break
other return  → continue

$.each([52, 97], fn)  // index 0 → 52, index 1 → 97
NeedUse
Array / object loop$.each(collection, fn)
DOM elements only$(sel).each(fn)
Build a new array$.map(collection, fn)

How to Use jQuery.each()

Pass a collection and a callback; optionally break with return false.

  1. Start with an array or plain object.
  2. Call $.each(collection, function (indexOrKey, value) { ... }).
  3. Use index/key and value (or this) inside the callback.
  4. Return false to stop early when needed.
PieceRequired?Role
collectionYesArray, array-like, or plain object
callbackYesRuns 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

Cheat Sheet

Keep these facts handy while iterating with jQuery.

TopicDetail
SyntaxjQuery.each(collection, callback)
SincejQuery 1.0
Array callbackfunction(index, value)
Object callbackfunction(key, value)
Breakreturn false
ReturnsOriginal collection
DOM cousin$(sel).each()

Live Preview

Pick a sample collection and watch $.each walk each entry.

Browser demo of $.each() — same idea as the examples below.

Result
0: 52
1: 97

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Iterate an Array

Official style: index and value for each item.

jQuery
$.each([52, 97], function (index, value) {
  console.log(index + ": " + value);
});
// 0: 52
// 1: 97
Try It Yourself

How It Works

  1. Collection. Pass a normal JavaScript array as the first argument.
  2. Callback. jQuery calls your function once per item with (index, value).
  3. First item. Index 0, value 52.
  4. Second item. Index 1, value 97.

Example 2 — Iterate Object Properties

Plain objects yield key/value pairs.

jQuery
var obj = {
  flammable: "inflammable",
  duh: "no duh"
};

$.each(obj, function (key, value) {
  console.log(key + ": " + value);
});
// flammable: inflammable
// duh: no duh
Try It Yourself

How It Works

  1. Object input. Named properties are iterated (not numeric indexes).
  2. Callback args. First parameter is the property name; second is its value.
  3. Order. Modern engines usually follow insertion order for string keys.
  4. Use case. Handy for config objects and dictionaries.

Example 3 — Break with return false

Stop the loop as soon as a condition matches.

jQuery
var lines = [];

$.each(["a", "b", "c", "d"], function (i, letter) {
  lines.push(letter);
  if (letter === "c") {
    return false; // break
  }
});

console.log(lines.join(", "));  // "a, b, c"
Try It Yourself

How It Works

  1. Push each letter until the stop condition.
  2. Break. When letter === "c", return false ends the loop.
  3. Skipped. "d" is never visited.
  4. Unlike forEach. Array.forEach cannot break this way.

Example 4 — Accumulate While Iterating

Build a total from array values.

jQuery
var prices = [9.99, 14.5, 3.25];
var total = 0;

$.each(prices, function (i, price) {
  total += price;
});

console.log(total.toFixed(2));  // "27.74"
Try It Yourself

How It Works

  1. Start at zero. total accumulates outside the callback.
  2. Add each price inside $.each.
  3. Format. toFixed(2) prints currency-style text.
  4. 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All 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.

Performance Notes

ApproachCostBest for
$.each(arr, fn)O(n)Arrays / objects with early exit
arr.forEach(fn)O(n)Arrays only (no break)
for / for…ofO(n)Hottest loops / fine control

For typical UI sizes, $.each is fine. Prefer native loops only when profiling shows iteration is hot.

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.

Next: map()

Learn how jQuery.map builds a new array from a collection.

map() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful