jQuery.each() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

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.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$.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
CallWhat it does
collectionArray, array-like, or plain object
callbackRuns once per item
Return valueSame collection (first argument)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Start with an array or plain object.
Step 2Call $.each(collection, function (indexOrKey, value) { ... }).
Step 3Use index/key and value (or this) inside the callback.

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

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.

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.

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.

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: jQuery.map() Method

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

jQuery.map() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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