jQuery jQuery.each() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Utility iteration

What You’ll Learn

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

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.

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.

📝 Syntax

General form of jQuery.each:

jQuery
jQuery.each( collection, callback(indexOrKey, value) )
// or
$.each( collection, callback(indexOrKey, value) )

Parameters

  • collection — an array, array-like object, or plain JavaScript object to iterate.
  • callback — a function invoked for each item. For arrays: (index, value). For objects: (key, value). Return false to break the loop.

Return value

  • Returns the same collection reference passed as the first argument.

Minimal workflow

jQuery
$.each([10, 20, 30], function (index, value) {

  console.log(index + ": " + value);

});

// 0: 10
// 1: 20
// 2: 30

⚡ Quick Reference

GoalCode
Iterate an array$.each(arr, function (i, val) { ... })
Iterate an object$.each(obj, function (key, val) { ... })
Break the loopreturn false; inside callback
Access current valueUse callback arg or this
Chain after iterate$.each(data, fn); // data unchanged
Long-form aliasjQuery.each(data, fn)

📋 $.each vs $(sel).each vs Array.forEach

Three iteration tools — similar names, different purposes.

$.each
any collection

Static utility; arrays, objects, array-like

$(sel).each
DOM only

Instance method on jQuery selections

forEach
arrays only

Native; no break, no object support

return false
break loop

Only $.each supports early exit this way

Examples Gallery

Each example uses $.each(). Open DevTools or use the Try-it links to run them interactively.

📚 Getting Started

Walk arrays and objects with a single callback.

Example 1 — Basic Array Iteration

Log each index and value from a simple numeric array — the classic jQuery.each() intro.

jQuery
$.each([52, 97], function (index, value) {

  console.log(index + ": " + value);

});
Try It Yourself

How It Works

For arrays, the callback receives a zero-based index and the element value. jQuery walks from index 0 to length - 1.

Example 2 — Iterate Object Properties

Walk key-value pairs on a plain JavaScript object — the callback signature switches to (key, value).

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



$.each(obj, function (key, value) {

  console.log(key + ": " + value);

});
Try It Yourself

How It Works

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

  }

});
Try It Yourself

How It Works

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));
Try It Yourself

How It Works

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));

});
Try It Yourself

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.

🚀 Use Cases

  • Config inspection — walk settings objects and apply defaults or validate keys.
  • Data aggregation — sum, filter, or transform arrays without manual index management.
  • Array-like objects — iterate NodeLists, arguments objects, or jQuery internal collections.
  • 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.

📝 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.

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 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
jQuery.each() Universal

Bottom line: Safe in any project that already includes jQuery. One of the most battle-tested utilities in the library.

🎉 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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about jQuery.each()

Iterate any collection the jQuery way.

5
Core concepts
[ ] 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.

Continue to jQuery.map()

Learn how to transform arrays and objects into a new array with $.map().

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.

6 people found this page helpful