jQuery merge() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

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

Syntax

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

jQuery
$.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)
CallWhat it does
jQueryLearn jQuery.merge(): append two arrays into the first, preserve order, clone with an empty array, vs concat and extend, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Prepare first (the array that will grow) and second (source to append).
Step 2Call $.merge(first, second).
Step 3Use the returned array (same reference as first).

Return Value

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

jQuery Examples

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.

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.

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.

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

Convert NodeLists and jQuery collections into true JavaScript arrays.

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

9 people found this page helpful