To just merge the arrays (without removing duplicates)

ES5 version use Array.concat:

var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];

array1 = array1.concat(array2);

console.log(array1);

2023 update

The original answer was from years ago. ES6 is fully supported and IE is finally dead. Here's the simplest way to merge primitive and object arrays:

const merge = (a, b, predicate = (a, b) => a === b) => {
    const c = [...a]; // copy to avoid side effects
    // add all items from B to copy C if they're not already present
    b.forEach((bItem) => (c.some((cItem) => predicate(bItem, cItem)) ? null : c.push(bItem)))
    return c;
}

merge(['a', 'b', 'c'], ['c', 'x', 'd']);
// => ['a', 'b', 'c', 'x', 'd']

merge([{id: 1}, {id: 2}], [{id: 2}, {id: 3}], (a, b) => a.id === b.id);
// [{id: 1}, {id: 2}, {id: 3}]

Original answer

ES6 version use destructuring

const array1 = ["Vijendra","Singh"];
const array2 = ["Singh", "Shakya"];
const array3 = [...array1, ...array2];

Since there is no 'built in' way to remove duplicates (ECMA-262 actually has Array.forEach which would be great for this), we have to do it manually. Note that this pollutes the Array prototype, use with caution.

Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);
        }
    }

    return a;
};

Then, to use it:

var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];
// Merges both arrays and gets unique items
var array3 = array1.concat(array2).unique(); 

This will also preserve the order of the arrays (i.e, no sorting needed).

Since many people are annoyed about prototype augmentation of Array.prototype and for in loops, here is a less invasive way to use it:

function arrayUnique(array) {
    var a = array.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);
        }
    }

    return a;
}

var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];
    // Merges both arrays and gets unique items
var array3 = arrayUnique(array1.concat(array2));

For those who are fortunate enough to work with browsers where ES5 is available, you can use Object.defineProperty like this:

Object.defineProperty(Array.prototype, 'unique', {
    enumerable: false,
    configurable: false,
    writable: false,
    value: function() {
        var a = this.concat();
        for(var i=0; i<a.length; ++i) {
            for(var j=i+1; j<a.length; ++j) {
                if(a[i] === a[j])
                    a.splice(j--, 1);
            }
        }

        return a;
    }
});
Answer from LiraNuna on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › concat
Array.prototype.concat() - JavaScript | MDN
The concat() method of Array instances is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
Top answer
1 of 16
2350

To just merge the arrays (without removing duplicates)

ES5 version use Array.concat:

var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];

array1 = array1.concat(array2);

console.log(array1);

2023 update

The original answer was from years ago. ES6 is fully supported and IE is finally dead. Here's the simplest way to merge primitive and object arrays:

const merge = (a, b, predicate = (a, b) => a === b) => {
    const c = [...a]; // copy to avoid side effects
    // add all items from B to copy C if they're not already present
    b.forEach((bItem) => (c.some((cItem) => predicate(bItem, cItem)) ? null : c.push(bItem)))
    return c;
}

merge(['a', 'b', 'c'], ['c', 'x', 'd']);
// => ['a', 'b', 'c', 'x', 'd']

merge([{id: 1}, {id: 2}], [{id: 2}, {id: 3}], (a, b) => a.id === b.id);
// [{id: 1}, {id: 2}, {id: 3}]

Original answer

ES6 version use destructuring

const array1 = ["Vijendra","Singh"];
const array2 = ["Singh", "Shakya"];
const array3 = [...array1, ...array2];

Since there is no 'built in' way to remove duplicates (ECMA-262 actually has Array.forEach which would be great for this), we have to do it manually. Note that this pollutes the Array prototype, use with caution.

Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);
        }
    }

    return a;
};

Then, to use it:

var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];
// Merges both arrays and gets unique items
var array3 = array1.concat(array2).unique(); 

This will also preserve the order of the arrays (i.e, no sorting needed).

Since many people are annoyed about prototype augmentation of Array.prototype and for in loops, here is a less invasive way to use it:

function arrayUnique(array) {
    var a = array.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);
        }
    }

    return a;
}

var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];
    // Merges both arrays and gets unique items
var array3 = arrayUnique(array1.concat(array2));

For those who are fortunate enough to work with browsers where ES5 is available, you can use Object.defineProperty like this:

Object.defineProperty(Array.prototype, 'unique', {
    enumerable: false,
    configurable: false,
    writable: false,
    value: function() {
        var a = this.concat();
        for(var i=0; i<a.length; ++i) {
            for(var j=i+1; j<a.length; ++j) {
                if(a[i] === a[j])
                    a.splice(j--, 1);
            }
        }

        return a;
    }
});
2 of 16
665

With Underscore.js or Lo-Dash you can do:

console.log(_.union([1, 2, 3], [101, 2, 1, 10], [2, 1]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

http://underscorejs.org/#union

http://lodash.com/docs#union

Discussions

How to combine two arrays in JavaScript
Combining arrays is really easy: var array3 = array1.concat(array2); Combining them in the way you specify here takes a bit more work... More on reddit.com
🌐 r/javascript
13
15
March 22, 2016
How can I merge two arrays of different objects but equal length?
Use the spread operator to merge objects, and do so in a loop: mergedArray = [] for (let i = 0; i < arrayOne.length; i++) { newObject = {...arrayOne[i], ...arrayTwo[i] } mergedArray.push(newObject) } More on reddit.com
🌐 r/learnjavascript
7
2
March 24, 2021
Help with typing a deep merge function.
I'm sure your running into a snag more complex than my suggestion, but if you already have a function that can deep merge two objects, couldn't you just recursively call that function against all the objects in the array? Or just use reduce and do the same thing? What are the roadblocks you are running into? More on reddit.com
🌐 r/typescript
10
7
November 9, 2020
How can I merge this array into one large ArrayBuffer?
I would try a forEach loop with either push for each element or splice if it push isn't working. More on reddit.com
🌐 r/learnjavascript
14
7
November 1, 2023
🌐
W3Schools
w3schools.com › jsref › jsref_concat_array.asp
JavaScript Array concat() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ... Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › join
Array.prototype.join() - JavaScript | MDN
The join() method of Array instances creates and returns a new string by concatenating all of the elements in this array, separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
🌐
JavaScript in Plain English
javascript.plainenglish.io › efficiently-merging-arrays-in-javascript-32993788a8b2
How to Efficiently Merge Arrays in JavaScript | by Chad Campbell | JavaScript in Plain English
February 18, 2022 - I was very surprised by what I saw. In this article, I’ll share what I learned. If you want a quick takeaway: The safest approach is to use the concat method of an Array object to merge two Arrays.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
5 days ago - Other methods (e.g., push(), splice(), etc.) also result in updates to an array's length property. ... When setting a property on a JavaScript array when the property is a valid array index and that index is outside the current bounds of the array, the engine will update the array's length property accordingly:
Find elsewhere
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-merge-arrays
3 Ways to Merge Arrays in JavaScript
January 28, 2023 - Write inside the array literal two or more arrays prefixed with the spread operator ..., and JavaScript will create a new array with all these arrays merged:
🌐
Medium
medium.com › @rivoltafilippo › javascript-merge-arrays-without-duplicates-3fbd8f4881be
Javascript merge arrays without duplicates | by Filippo Rivolta | Medium
April 12, 2021 - Using ES5 we can merge the two input arrays without removing the duplicates with .concat() and then loop again through the result array and remove duplicates using indexOf.
🌐
Reddit
reddit.com › r/javascript › how to combine two arrays in javascript
r/javascript on Reddit: How to combine two arrays in JavaScript
March 22, 2016 -

I'm just wondering how I would go about combing two arrays in JavaScript into a 3rd array. It sounds simple, but I'm finding it really hard.

Array1 = [
    [
        "q0",
        "a0"
    ],
    [
        "q1",
        "a1"
    ]
];

Array2 = [
    [
        "q0",
        "w0"
    ],
    [
        "q1",
        "w1"
    ]
];

Ideally, I want this in the format of:

array3 = [
    [
        [
        "q0",
        "a0",
        "w0"
    ],
    [
        "q1",
        "a1",
        "w1"
    ]
];

Any help would be appreciated, thanks

🌐
Mimo
mimo.org › glossary › javascript › array-concatenation
JavaScript Array Concatenate: Syntax, Usage, and Examples
Merge arrays in JavaScript using concat(), the spread operator (...), or push(). Quickly combine lists, results, or values without loops.
🌐
Greenroots
blog.greenroots.info › 5-ways-to-merge-arrays-in-javascript-and-their-differences
5 ways to merge arrays in JavaScript and their differences
October 15, 2021 - We use arrays to store data as elements and retrieve them back when we need them. The array is a data structure widely used in many programming languages, and JavaScript is not an exception. You may need to merge one or more arrays to combine all the elements from the individual arrays into ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-merge-arrays-in-javascript-array-concatenation-in-js
How to Merge Arrays in JavaScript – Array Concatenation in JS
November 28, 2022 - The spread operator allows you to spread an iterable collection (object or array) into another collection. Using this operator on arrays, you can merge the contents of arrays together.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › concat
JavaScript Array concat() - Merge Arrays Together | Vultr Docs
November 28, 2024 - The concat() method in JavaScript is a critical tool for combining two or more arrays into a single array without altering the original arrays. This method provides a straightforward way to manage array data, particularly useful in scenarios ...
🌐
Codedamn
codedamn.com › news › javascript
JavaScript concat array explained – How to merge arrays in JS
June 24, 2023 - In this example, array1 contains three numbers, while array2 contains three strings. The JavaScript concat() method is used to merge two or more arrays into a single array.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-merge-two-arrays-without-creating-a-new-array
How to Merge Two Arrays Without Creating a New Array in JavaScript? - GeeksforGeeks
August 5, 2025 - We can use array concat() method to Merge Two Arrays Without Creating a New Array. It is a very basic method to merge arrays.
🌐
David Walsh
davidwalsh.name › merge-arrays-javascript
Merge Arrays with JavaScript
July 9, 2015 - Arrays can be merged using JavaScript's Array.prototype.push.
🌐
Go Make Things
gomakethings.com › merging-arrays-and-objects-with-vanilla-javascript
Merging arrays and objects with vanilla JavaScript | Go Make Things
April 1, 2023 - Today, we’re going to talk about how to merge multiple arrays or objects together. Let’s dig in! Merging arrays You can use the Array.prototype.concat() method to merge two or more arrays together.
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
Merge Arrays in JavaScript: With and Without Duplicates | Envato Tuts+
February 19, 2023 - ES6 offers a single-line solution for merging arrays without duplicates. It uses the spread operator and a Set. Set is a built-in data structure in JavaScript ES6 and above that does not support duplicates. This is where concat() and Set deviate—Set checks for duplicates and removes them.
🌐
jQuery
api.jquery.com › jQuery.merge
jQuery.merge() | jQuery API Documentation
This shortcut creates a new, empty array and merges the contents of oldArray into it, effectively cloning the array. Prior to jQuery 1.4, the arguments should be true Javascript Array objects; use $.makeArray if they are not.
🌐
LinkedIn
linkedin.com › pulse › how-efficiently-merge-arrays-javascript-data-ins-technology-llc
How to Efficiently Merge Arrays in JavaScript
July 5, 2023 - Using the reduce() method: The reduce() method can also be used to merge arrays efficiently. It iterates over the arrays, applying a reducing function to combine them into a single array. javascript const arrays = [[1, 2], [3, 4], [5, 6]]; const ...