jQuery .map() Method

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is .map()?

.map() (since jQuery 1.2) runs a callback on every matched element and builds a new jQuery object from the values you return. Ideal for collecting IDs, values, text, or numbers from a set.

Remember
// Collect checkbox IDs → plain array → joined string
$(":checkbox")
  .map(function () {
    return this.id;
  })
  .get()
  .join();

// Skip an item: return null or undefined
// Expand: return an array of items
NeedUse
Transform matched elements$("li").map(fn)
Side effects only (no new set)$("li").each(fn)
Map a plain array / objectjQuery.map(arr, fn)
Unwrap to a JS array.get() or .toArray()
Keep / drop by test.filter()

How to Use .map()

Select elements, return a value per item, then unwrap if you need a plain array.

  1. Select the elements to transform.
  2. Call .map(function (index, element) { … }).
  3. Inside the callback, this is the current DOM element.
  4. Return a value, an array of values, or null/undefined to skip.
  5. Chain .get() when you need a native array (.join(), Math.max, …).
SignatureSinceRole
.map(callback)1.2Build a new jQuery object from return values
callback(index, element)—this = current DOM element
Return value—Inserted into the result set
Return array—Each item inserted (expand)
Return null / undefined—Skip — nothing inserted

Pseudocode

Pseudocode
result = empty jQuery object

for each matched element with index i:
    value = callback.call(element, i, element)

    if value is null or undefined:
        skip
    else if value is an array:
        append each item in value to result
    else:
        append value to result

return result
// often: result.get() → plain array

Cheat Sheet

Keep these facts handy while transforming collections.

TopicDetail
ReturnsNew jQuery object (not the original set)
vs .each().each() = side effects · .map() = collect values
vs jQuery.map()Static utility for arrays/objects
Common chain.map(…).get().join()
SkipReturn null or undefined
ExpandReturn an array of items
thisCurrent DOM element

Live Preview

Map the sample list — collect text, skip odd indexes, or uppercase.

Sample set: the three list items below.

  • Alpha
  • Beta
  • Gamma
Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Prefer .map() when you need collected values.

Example 1 — Collect Checkbox IDs

Official pattern: map ids, then .get().join().

jQuery
var ids = $(":checkbox").map(function () {
  return this.id;
}).get().join();

$("#out").text("IDs: " + ids);
// → "IDs: two,four,six,eight"
Try It Yourself

How It Works

  1. Select. $(":checkbox") finds every checkbox.
  2. Map. Return this.id for each one.
  3. Unwrap. .get() gives a plain array.
  4. Join. .join() builds a comma-separated string.

Example 2 — Collect Form Input Values

Official Ex 1 idea: map .val() from every input, then join.

jQuery
$("p").append(
  $("input").map(function () {
    return $(this).val();
  }).get().join(", ")
);
// Values: 2, 4, 6, 8
Try It Yourself

How It Works

  1. Select. All input fields in the form.
  2. Read. $(this).val() returns each field value.
  3. Collect. .map() builds a jQuery-wrapped list of strings.
  4. Show. .get().join(", ") appends readable text.

Example 3 — Skip with null and Expand with an Array

Return null to drop an item; return an array to insert more than one.

jQuery
var items = $("li").map(function (index) {
  var text = $(this).text();
  if (index === 1 || index === 3) {
    return null; // skip Second, Fourth
  }
  if (index === 2) {
    return [text, text + " Extra"]; // expand Third
  }
  return text;
}).get();

$("#out").text(items.join(" | "));
// First | Third | Third Extra | Fifth
Try It Yourself

How It Works

  1. Skip. null/undefined inserts nothing.
  2. Expand. An array inserts each of its items.
  3. Keep. A single return value inserts one item.
  4. Unwrap. .get() for a plain array before joining.

Example 4 — Map Numbers, Then Find the Max

Official Ex 3 idea without layout CSS: map lengths, then Math.max.

jQuery
var lengths = $("li").map(function (i, el) {
  return $(el).text().length;
}).get();

var maxLen = Math.max.apply(null, lengths);
$("#out").text("Lengths: " + lengths.join(", ") + " | max=" + maxLen);
// Lengths: 5, 10, 3 | max=10
Try It Yourself

How It Works

  1. Map. Return a number from each element.
  2. Unwrap. .get() → native number array.
  3. Reduce. Math.max.apply(null, lengths) finds the largest.
  4. Same idea as equalizing heights — map metrics, then apply a max.

Example 5 — .map() vs .each()

Map collects text; each applies a side effect (mark odd items).

jQuery
var texts = $("#demo li").map(function () {
  return $(this).text();
}).get().join(" | ");

$("#out").text("Mapped: " + texts);

$("#demo li").each(function (i) {
  if (i % 2) {
    $(this).prepend("[odd] ");
  }
});
Try It Yourself

How It Works

  1. Map. Builds a new list of return values.
  2. Each. Changes elements in place; returns the same set.
  3. Rule. Need results → .map(); need side effects → .each().
  4. CSS-free. Text markers show the effect without styles.

Edge Cases & Pitfalls

These trip up beginners most often with .map().

unwrap

Forgetting .get()

.map() returns a jQuery object. Call .get() (or .toArray()) before native array methods like .join().

skip

null / undefined skip silently

Useful for filtering inside .map(), but easy to miss if you meant to return 0 or "" — those are kept.

names

Wrong map API

DOM collections → $("…").map(). Plain arrays/objects → jQuery.map(). Mixing them causes confusing results.

each

Using .map() only for side effects

If you never use the return value, prefer .each(). It documents intent and avoids building an unused result set.

Browser Support

.map() has been part of jQuery since 1.2. Works wherever your loaded jQuery build runs.

✓ Stable · jQuery 1.2+

jQuery .map()

Universal across browsers that run your jQuery version. Prefer current jQuery 3.x+ for teaching and production.

100% jQuery core
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
.map() Universal

Bottom line: Safe for everyday collection transforms. Chain .get() when you need a plain array.

Performance Notes

ApproachCostBest for
.map() on a small setCheapForms, lists, typical UI
.map() + .get()CheapFeeding native array APIs
Huge DOM + heavy work per itemWatchNarrow the selector first

Cost scales with match count and callback work. Prefer a tight selector over mapping half the document.

Key Takeaways

  • Role: transform matched elements into a new jQuery object of return values.
  • Unwrap: chain .get() before .join() or other native array methods.
  • Skip / expand: null/undefined skips; an array inserts multiple items.
  • Choose: results → .map() · side effects → .each() · plain data → jQuery.map().

One line: .map() turns each matched element into a collected value — then .get() when you need a plain array.

Frequently Asked Questions

.map(callback) passes each element in the current matched set through a function and builds a new jQuery object from the return values. Available since jQuery 1.2.
.each() runs a callback for side effects and returns the original collection. .map() collects return values into a new jQuery object. Use .each() to change elements; use .map() when you need transformed results.
$('li').map(fn) loops matched DOM elements. jQuery.map(arrayOrObject, fn) is a static utility for plain arrays or objects. This page covers the instance method.
.map() returns a jQuery object wrapping results. .get() unwraps it to a plain JavaScript array — needed before .join(), Math.max.apply, or other native array APIs.
Nothing is inserted for that iteration — the item is skipped. Returning an array inserts each item inside it, so you can filter or expand inside .map().
this is the current DOM element, same as .each(). The callback also receives (index, domElement).

Next: .next() Method

Get the immediately following sibling of each matched element.

.next() 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.

18 people found this page helpful