.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
Select elements, return a value per item, then unwrap if you need a plain array.
Select the elements to transform.
Call .map(function (index, element) { … }).
Inside the callback, this is the current DOM element.
Return a value, an array of values, or null/undefined to skip.
Chain .get() when you need a native array (.join(), Math.max, …).
Signature
Since
Role
.map(callback)
1.2
Build 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
Reference
Cheat Sheet
Keep these facts handy while transforming collections.
Topic
Detail
Returns
New 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()
Skip
Return null or undefined
Expand
Return an array of items
this
Current DOM element
Try it
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.
Code
jQuery Examples
Short, copy-ready snippets. Prefer .map() when you need collected values.
Collect..map() builds a jQuery-wrapped list of strings.
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
Each. Changes elements in place; returns the same set.
Rule. Need results → .map(); need side effects → .each().
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.
Compatibility
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 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
.map()Universal
Bottom line: Safe for everyday collection transforms. Chain .get() when you need a plain array.
Analysis
Performance Notes
Approach
Cost
Best for
.map() on a small set
Cheap
Forms, lists, typical UI
.map() + .get()
Cheap
Feeding native array APIs
Huge DOM + heavy work per item
Watch
Narrow the selector first
Cost scales with match count and callback work. Prefer a tight selector over mapping half the document.
Remember
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).