The jQuery.merge() utility appends every element from a second array-like collection onto the end of the first. This tutorial covers syntax, destructive vs safe merge patterns, five worked examples, comparisons with Array.concat() and $.extend(), and when duplicates are preserved.
01
Syntax
$.merge(a, b)
02
Append
Second onto first
03
Order
Both preserved
04
Clone
$.merge([], arr)
05
Destructive
First modified
06
Dupes
Not removed
Fundamentals
Introduction
Combining two lists of data is a daily task — paginated API results, selected checkboxes, or DOM node collections converted to arrays. jQuery provides jQuery.merge() (also written $.merge()) to append one array-like collection onto another while preserving element order.
Unlike jQuery.extend(), which merges object properties by key, $.merge() works on indexed collections. It is the jQuery-era counterpart to appending arrays — with one important caveat: the first array is modified in place.
Concept
Understanding the jQuery.merge() Method
jQuery.merge(first, second) copies each element from second and appends it to first. The method updates first.length and returns the same first reference. second is never altered.
Duplicate values are not deduplicated — if both arrays contain 2, the result contains 2 twice. Choose $.merge() when you want a fast in-place append; choose Array.concat() or the clone pattern when immutability matters.
💡
Beginner Tip
To clone an array with jQuery, use $.merge([], oldArray). The empty array receives a copy of every element — a handy shortcut documented on the jQuery API page.
Foundation
📝 Syntax
General form of jQuery.merge:
jQuery
jQuery.merge( first, second )
// or
$.merge( first, second )
Parameters
first — the first array-like object. Elements from second are appended here. This object is modified.
second — the second array-like object. Its elements are appended to first without changing second itself.
Return value
Returns the modified first array (same reference, now longer).
Minimal workflow
jQuery
var list = [1, 2, 3];
$.merge(list, [4, 5]);
console.log(list); // [1, 2, 3, 4, 5]
Cheat Sheet
⚡ Quick Reference
Goal
Code
Append arrays
$.merge(first, second)
Clone an array
$.merge([], arr)
Merge without mutating sources
$.merge($.merge([], first), second)
Remove duplicates?
No — all elements kept
Second array changed?
No — only first is modified
Immutable alternative
first.concat(second)
Compare
📋 $.merge vs concat vs $.extend
Three merge-sounding tools — different data shapes and side effects.
$.merge
arrays
Append elements; mutates first
concat
new array
Native; both sources unchanged
$.extend
objects
Merge properties by key
Clone
$.merge([],a)
Shallow array copy shortcut
Hands-On
Examples Gallery
Each example uses $.merge(). Open DevTools or use the Try-it links to run them interactively.
📚 Getting Started
Append one array onto another — duplicates included.
Example 1 — Basic Merge with Duplicates
Merge [0, 1, 2] and [2, 3, 4] — the shared 2 appears twice.
jQuery
var first = [0, 1, 2];
$.merge(first, [2, 3, 4]);
console.log(first); // [0, 1, 2, 2, 3, 4]
This matches the second official jQuery API example. Order in both source arrays is fully preserved in the result — only the boundary between them shifts.
Example 3 — Clone an Array with an Empty Target
Use $.merge([], oldArray) to shallow-copy every element into a new array.
jQuery
var original = ["a", "b", "c"];
var copy = $.merge([], original);
console.log(copy); // ["a", "b", "c"]
console.log(copy === original); // false
The empty array is the merge target — it receives all elements from original without modifying original. This is a documented jQuery idiom predating widespread use of spread syntax.
Example 4 — Merge Without Altering Either Source
Nested merge into empty arrays — the official pattern for a non-destructive combine.
jQuery
var first = ["a", "b", "c"];
var second = ["d", "e", "f"];
var combined = $.merge($.merge([], first), second);
console.log(combined); // ["a","b","c","d","e","f"]
console.log(first); // ["a","b","c"] (unchanged)
console.log(second); // ["d","e","f"] (unchanged)
The inner $.merge([], first) clones first; the outer merge appends second. Both originals remain intact — ideal when you need a new combined array for rendering or further processing.
Example 5 — Combine Paginated API Results
Accumulate rows from multiple fetch pages into one in-memory list.
Each AJAX callback can $.merge(allRows, newPage) to grow the master list in place. For immutable pipelines, use allRows = allRows.concat(newPage) instead.
Applications
🚀 Common Use Cases
Paginated data — accumulate rows from multiple API pages into one list.
Shallow clone — $.merge([], arr) when you need a quick array copy in legacy jQuery code.
Batch DOM results — combine arrays built from separate DOM queries or $.makeArray() conversions.
In-place accumulation — grow a working buffer without allocating a new array each iteration.
Non-destructive combine — nested empty-array merge when both sources must stay unchanged.
🧠 How jQuery.merge() Combines Arrays
1
Read first length
jQuery notes the current first.length as the append offset.
Start
2
Copy second items
Each element from second is placed at the next index in first.
Append
3
Update length
first.length grows to include all appended elements.
Resize
4
📦
Return first
The modified first reference is returned; second is untouched.
Important
📝 Notes
Available since jQuery 1.0; array-like arguments supported throughout modern jQuery.
Prior to jQuery 1.4, arguments had to be true arrays — use $.makeArray() to convert NodeLists or other collections.
$.merge() performs a shallow copy of references — nested objects inside elements are not cloned.
Do not confuse with $.extend() — that merges object keys, not array indices.
For deduplication, merge first then filter — $.merge does not remove duplicates.
Modern code often prefers [...a, ...b] or a.concat(b) for immutable merges.
Compatibility
Browser Support
jQuery.merge() 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.merge()
Supported in jQuery 1.x, 2.x, and 3.x. Behavior is consistent across browsers because jQuery owns the merge implementation.
100%With jQuery loaded
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
jQuery.merge()Universal
Bottom line: Safe in any project that already includes jQuery. For greenfield apps without jQuery, native concat or spread syntax covers the same use cases immutably.
Wrap Up
Conclusion
The jQuery.merge() utility appends one array-like collection onto another. Remember that it modifies the first argument, keeps duplicates, and leaves the second array unchanged.
Use $.merge([], arr) to clone, or the nested empty-array pattern when both sources must survive untouched.
Use nested merge when both arrays must stay unchanged
Prefer concat or spread when immutability is required
Convert NodeLists with $.makeArray() on very old jQuery if needed
Chain merges to accumulate paginated results in one buffer
❌ Don’t
Expect $.merge to deduplicate values
Confuse $.merge with $.extend for objects
Assume the first array stays unchanged after merge
Deep-clone nested objects via merge — it copies references only
Merge plain objects — use $.extend instead
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about jQuery.merge()
Combine array-like collections the jQuery way.
5
Core concepts
➕01
$.merge
Append arrays
API
⚠02
Mutates first
In-place
Side effect
🔄03
Keeps dupes
No dedupe
Behavior
📄04
Clone
$.merge([], a)
Pattern
📑05
Not extend
Arrays only
Scope
❓ Frequently Asked Questions
jQuery.merge(first, second) appends all elements from the second array-like object onto the end of the first. It modifies the first array in place, updates its length, and returns the merged first array. The second array is left unchanged.
concat() returns a new array without modifying either source. $.merge() is destructive — it alters the first array. Use $.merge([], arr) or concat when you need to preserve the original first array.
jQuery.extend() merges object properties by key. jQuery.merge() appends array elements by index — it concatenates two array-like collections, not plain object key/value pairs.
Clone first with an empty array: var result = $.merge($.merge([], first), second). The nested pattern copies first into a new array, then appends second.
Yes. Both arguments can be array-like objects with a length property and indexed values — not just true arrays. Prior to jQuery 1.4, use $.makeArray() to convert non-array collections first.
Did you know?
The jQuery API documents that $.merge() itself can clone arrays: var newArray = $.merge([], oldArray). That single line creates a new array and copies every element — a pattern jQuery developers used for years before [...arr] became common.