jQuery merge() Method

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

What Is jQuery.merge()?

jQuery.merge(first, second) (alias $.merge) appends items from second onto the end of first, preserves order, and returns the updated first array (since jQuery 1.0).

Remember
$.merge(first, second)  → first (modified)

$.merge([0, 1, 2], [2, 3, 4])
→ [0, 1, 2, 2, 3, 4]   // duplicates kept

// Clone:
$.merge([], oldArray)

// Merge without changing sources:
$.merge($.merge([], first), second)
NeedUse
Append into first (in place)$.merge(first, second)
New array, both unchangedfirst.concat(second)
Merge object keys$.extend(a, b)

How to Use jQuery.merge()

Pass two array-like values; second is appended onto first.

  1. Prepare first (the array that will grow) and second (source to append).
  2. Call $.merge(first, second).
  3. Use the returned array (same reference as first).
  4. If you must keep first unchanged, merge into a clone: $.merge($.merge([], first), second).
PieceRequired?Role
firstYesTarget array-like — modified in place
secondYesItems to append — left unaltered
Return—The merged first array

Pseudocode

Pseudocode
function merge(first, second):
    for each item in second:
        append item to first
    update first.length
    return first

// Official clone shortcut:
newArray = merge([], oldArray)

// Official safe combine:
result = merge(merge([], first), second)

Cheat Sheet

Keep these facts handy while combining arrays.

TopicDetail
SyntaxjQuery.merge(first, second)
SincejQuery 1.0
Mutates?Yes — first only
OrderPreserved; second appended
DuplicatesKept (not unique)
Clone$.merge([], arr)
Pre-1.4True arrays; else $.makeArray

Live Preview

Run official-style $.merge demos: append, clone, or safe combine.

Browser demo of $.merge() — same idea as the examples below.

Result
$.merge([0, 1, 2], [2, 3, 4])
→ [0, 1, 2, 2, 3, 4]

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Basic Merge

Append the second array onto the first — duplicates stay.

jQuery
var first = [0, 1, 2];
$.merge(first, [2, 3, 4]);

console.log(first); // [0, 1, 2, 2, 3, 4]
Try It Yourself

How It Works

  1. Target. first starts as [0, 1, 2].
  2. Append. Items from [2, 3, 4] are pushed onto the end.
  3. Duplicates. The shared 2 appears twice — merge does not unique.
  4. Mutated. first itself is the result array.

Example 2 — Order Is Preserved

First-array items stay in front; second is appended in its own order.

jQuery
var first = [3, 2, 1];
$.merge(first, [4, 3, 2]);

console.log(first); // [3, 2, 1, 4, 3, 2]
Try It Yourself

How It Works

  1. Keep front. 3, 2, 1 stay at the start.
  2. Append. 4, 3, 2 follow in the same order.
  3. No sort. Merge never reorders for uniqueness or sorting.
  4. Use case. Good when sequence of pages or chunks matters.

Example 3 — Clone with an Empty Array

Official shortcut: merge into [] to shallow-copy.

jQuery
var original = ["a", "b", "c"];
var copy = $.merge([], original);

console.log(copy);                 // ["a", "b", "c"]
console.log(copy === original);    // false
Try It Yourself

How It Works

  1. Empty target. Start with a brand-new [].
  2. Copy. Items from original are appended into that new array.
  3. Separate. copy === original is false.
  4. Shallow. Nested objects are still shared by reference.

Example 4 — Merge Without Changing Sources

Nest two merges so both originals stay intact.

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"]
console.log(second);   // ["d", "e", "f"]
Try It Yourself

How It Works

  1. Inner merge. $.merge([], first) clones first.
  2. Outer merge. Appends second onto that clone.
  3. Sources safe. Neither first nor second is mutated.
  4. Alternative. first.concat(second) is often clearer for this case.

Example 5 — Accumulate Paginated Rows

Grow one results list as each page arrives.

jQuery
var allRows = ["Row 1", "Row 2"];
$.merge(allRows, ["Row 3", "Row 4"]);
$.merge(allRows, ["Row 5"]);

console.log(allRows.join(" | "));
// "Row 1 | Row 2 | Row 3 | Row 4 | Row 5"
Try It Yourself

How It Works

  1. Accumulator. allRows is the growing first array.
  2. Page 2. Merge appends the next chunk in place.
  3. Page 3. Another merge adds the final row.
  4. Why merge. Useful when you intentionally want one mutable list.

Edge Cases & Pitfalls

Check these before merging arrays in real projects.

mutate

First array is modified

Always treat first as mutated. Clone first if callers still need the original list.

dupes

Duplicates are kept

Merge is not unique. Use $.uniqueSort() only for DOM node arrays when you need document-order uniqueness.

vs extend

Not for plain objects

Object key merges belong to $.extend(). $.merge appends indexed items.

legacy

Pre-1.4 needs true arrays

Older jQuery required real arrays. Convert with $.makeArray() when supporting very old builds.

Browser Support

jQuery.merge() needs jQuery in the browser. It has been available since jQuery 1.0+ in all browsers your jQuery build supports. Array-like arguments are accepted in modern jQuery (1.4+).

✓ jQuery 1.0+ · Browsers

jQuery.merge()

Works in Chrome, Firefox, Safari, Edge, and other browsers with jQuery loaded. Append array-like collections into the first array in place.

100% Browser support
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
$.merge() Browsers

Bottom line: Use $.merge(first, second) to append in place — clone with $.merge([], arr) when you need a copy.

Performance Notes

ApproachCostBest for
$.merge(a, b)O(|b|)In-place append / accumulators
a.concat(b)O(|a|+|b|)Immutable combine
$.merge([], a)O(|a|)Shallow clone

Prefer in-place merge when building one growing list. Prefer concat when immutability matters more than reuse of the first array.

Key Takeaways

  • Role: $.merge(first, second) appends onto first.
  • Destructive: only first changes; second is untouched.
  • Clone: $.merge([], arr) or nest merges for a safe combine.
  • Not unique: duplicates are kept; order is preserved.

One line: $.merge(first, second) appends second onto first in place — clone first when you need the original.

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.
No. Duplicate values are kept. $.merge([0, 1, 2], [2, 3, 4]) produces [0, 1, 2, 2, 3, 4] — the shared 2 appears twice.
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.

Next: makeArray()

Convert NodeLists and jQuery collections into true JavaScript arrays.

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

9 people found this page helpful