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
Then, for each argument, its value will be concatenated into the array — for normal objects or primitives, the argument itself will become an element of the final array; for arrays or array-like objects with the property Symbol.isConcatSpreadable set to a truthy value, each element of the argument will be independently added to the final array.
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-concat-method
JavaScript Array concat() Method - GeeksforGeeks
[Example 1]: Below the Array concat() method to join three arrays. JavaScript ·
Published   December 30, 2017
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

🌐
Mimo
mimo.org › glossary › javascript › array-concatenation
JavaScript Array Concatenate: Syntax, Usage, and Examples
The two most common and recommended ways to concatenate arrays in JavaScript are the concat() method and the spread operator (...). Both create a new array without changing the original arrays.
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-merge-arrays
3 Ways to Merge Arrays in JavaScript
January 28, 2023 - JavaScript offers multiple ways to merge arrays. You can use either the spread operator [...array1, ...array2], or a functional way [].concat(array1, array2) to merge 2 or more arrays.
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript array concat method
JavaScript Array concat Method
September 1, 2008 - JavaScript Array concat() Method one,two,three,four,five,six · In this example, we are concatenating more than two JavaScript arrays −
🌐
SamanthaMing
samanthaming.com › tidbits › 49-2-ways-to-merge-arrays
2 Ways to Merge Arrays in JavaScript | SamanthaMing.com
Here are 2 ways to combine your arrays and return a NEW array. I like using the Spread operator. But if you need older browser support, you should use Concat.
Find elsewhere
🌐
TechOnTheNet
techonthenet.com › js › array_concat.php
JavaScript: Array concat() method
var totn_array1 = ['Tech','On']; var totn_array2 = ['The','Net']; console.log(totn_array1.concat(totn_array2)); In this example, we have declared two array objects called totn_array1 and totn_array2, each with 2 elements.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › array.prototype.concat()
JavaScript Array concat: Merge Arrays
November 6, 2024 - ... let odds = [1, 3, 5]; let evens = [2, 4, 6]; let results = [].concat(odds, evens); console.log({ results });Code language: JavaScript (javascript) ... let upper = ['A', 'B', 'C']; let lower = ['a', 'b', 'c']; let digits = [1, 2, 3]; let ...
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › concat
JavaScript Array concat() - Merge Arrays Together | Vultr Docs
November 28, 2024 - The resulting array is ['apple', 'banana', 'cherry', 'date']. Understand that concat() can merge more than two arrays at a time.
🌐
Programiz
programiz.com › javascript › library › array › concat
JavaScript Array concat()
[ 'JavaScript', 'Python', 'Java', 'C', 'C++' ] [ 'C', 'C++', 'Lua', 'JavaScript', 'Python', 'Java' ] The concat() method returns the shallow copy of the concatenated elements in the following way: It copies object references to the new array. (For example: passing a nested array) So if the referenced object is modified, the changes are visible in the returned new array.
🌐
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.
🌐
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.
🌐
Dillion's Blog
dillionmegida.com › p › array-concat
Array concat method simplified - Dillion's Blog
#javascript · Here's a video if you'd prefer that: https://youtu.be/a5kkO4KMvUE · The concat method in JavaSript is used to concatenate the contents of an existing array with new values to form a new array. I'll explain, with examples, how this method works on different values.
🌐
W3Resource
w3resource.com › javascript › object-property-method › array-concat.php
JavaScript concat() Method : Array Object - w3resource
Implemented in JavaScript 1.2 · ... be joined to an array. Example -1: In the following web document two arrays arrayname1 and arrayname2 with single element have joined by concat() method....
🌐
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 - If you are sure the inputs are all arrays, please use the spread operator. It is a very straightforward and modern way to merge arrays. But if you are unsure about the input element type, use the concat() method. For example, let's take a string tapas and use the spread operator on it with the array literals,
🌐
Javatpoint
javatpoint.com › javascript-array-concat-method
JavaScript Array concat() Method
This method does not affect the original array. Syntax array.; Parameter It does not hold any parameter. Return It returns a new array iterator object. JavaScript Array Example Let&#39;s...
🌐
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 - By Dillion Megida There are multiple ways to merge arrays in JavaScript. You can use long or short approaches. I'll be showing 3 of them in this article. When working with arrays in JavaScript, there are cases where you want to combine multiple array...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › concat
String.prototype.concat() - JavaScript | MDN
const str1 = "Hello"; const str2 = "World"; console.log(str1.concat(" ", str2)); // Expected output: "Hello World" console.log(str2.concat(", ", str1)); // Expected output: "World, Hello"