Table of Contents

JavaScript Array concat() Method is used to combine two or more arrays into a single new array. It allows developers to join arrays without changing the original arrays. This makes it useful when working with lists of values, combining data from multiple sources, or building a new collection from existing arrays.

For example, suppose you have two arrays containing programming languages and frameworks. You can use concat() to combine them into one array without manually copying each element.

const languages = ["JavaScript", "Python", "Java"];
const frameworks = ["React", "Angular", "Vue"];

const result = languages.concat(frameworks);

console.log(result);

Output:

["JavaScript", "Python", "Java", "React", "Angular", "Vue"]

The concat() method returns a new array containing the elements from the original array followed by the elements from the supplied arrays or values. The original arrays remain unchanged.

Syntax of the concat() Method

The syntax of the JavaScript concat() method is straightforward:

array.concat(value1, value2, ..., valueN);

Parameters:

  • value1: An array or value to add to the end of the original array.
  • value2: An optional additional array or value.
  • valueN: Any number of additional arrays or values.

The method accepts zero or more arguments. If an argument is an array, its elements are added individually to the resulting array. If an argument is a non-array value, that value is added as a single element.

Return value:

The concat() method returns a new array containing the combined elements. It does not directly modify the original array.

Basic Example of concat()

The simplest way to use concat() is to combine two arrays.

const firstArray = [10, 20, 30];
const secondArray = [40, 50, 60];

const combinedArray = firstArray.concat(secondArray);

console.log(combinedArray);

Output:

[10, 20, 30, 40, 50, 60]

In this example, firstArray contains three numbers, and secondArray contains another three numbers. The concat() method combines their elements into a new array named combinedArray.

You can verify that the original arrays are unchanged:

console.log(firstArray);
console.log(secondArray);

Output:

[10, 20, 30]
[40, 50, 60]

This behavior makes concat() useful when you need to combine data while preserving the original collections.

Combining Multiple Arrays

The concat() method can combine more than two arrays in a single operation. You do not need to call the method repeatedly.

const frontend = ["HTML", "CSS", "JavaScript"];
const backend = ["Node.js", "PHP", "Python"];
const database = ["MySQL", "MongoDB", "PostgreSQL"];

const technologies = frontend.concat(backend, database);

console.log(technologies);

Output:

[
  "HTML",
  "CSS",
  "JavaScript",
  "Node.js",
  "PHP",
  "Python",
  "MySQL",
  "MongoDB",
  "PostgreSQL"
]

Here, three arrays are combined into one array. The elements appear in the same order in which the arrays are supplied to concat().

You can also combine several arrays with a single original array:

const groupA = [1, 2];
const groupB = [3, 4];
const groupC = [5, 6];
const groupD = [7, 8];

const allGroups = groupA.concat(groupB, groupC, groupD);

console.log(allGroups);

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

This approach is useful when data is separated into multiple arrays but needs to be processed as one collection.

Adding Individual Values with concat()

The concat() method does not require every argument to be an array. You can also pass individual values.

const numbers = [10, 20, 30];

const result = numbers.concat(40, 50, 60);

console.log(result);

Output:

[10, 20, 30, 40, 50, 60]

The individual values are appended to the new array in the order they are provided.

You can combine arrays and individual values in the same method call:

const fruits = ["Apple", "Banana"];
const moreFruits = ["Mango", "Orange"];

const result = fruits.concat("Grapes", moreFruits, "Pineapple");

console.log(result);

Output:

[
  "Apple",
  "Banana",
  "Grapes",
  "Mango",
  "Orange",
  "Pineapple"
]

This example demonstrates that concat() can accept a mixture of arrays and individual values.

Using concat() with Empty Arrays

Calling concat() without arguments creates a shallow copy of the original array.

const original = [10, 20, 30, 40];

const copied = original.concat();

console.log(copied);

Output:

[10, 20, 30, 40]

The returned array contains the same elements as the original array, but it is a different array object.

console.log(original === copied);

Output:

false

The comparison returns false because both variables refer to different array objects.

This can be useful when you need a new array containing the same elements without modifying the original array.

Combining Arrays of Objects

JavaScript arrays frequently contain objects instead of simple values. The concat() method can combine these arrays as well.

const employeesA = [
  { id: 1, name: "John" },
  { id: 2, name: "David" }
];

const employeesB = [
  { id: 3, name: "Sarah" },
  { id: 4, name: "Michael" }
];

const allEmployees = employeesA.concat(employeesB);

console.log(allEmployees);

Output:

[
  { id: 1, name: "John" },
  { id: 2, name: "David" },
  { id: 3, name: "Sarah" },
  { id: 4, name: "Michael" }
]

The method combines the object references into a new array. It does not create new copies of the individual objects.

For example:

allEmployees[0].name = "Jonathan";

console.log(employeesA[0].name);

Output:

Jonathan

The original object is affected because both arrays refer to the same object. This is an important detail when working with arrays of objects.

concat() Does Not Modify the Original Array

One of the most important characteristics of concat() is that it does not mutate the original array.

const numbers = [1, 2, 3];
const extraNumbers = [4, 5, 6];

const combined = numbers.concat(extraNumbers);

console.log(combined);
console.log(numbers);

Output:

[1, 2, 3, 4, 5, 6]
[1, 2, 3]

The first output displays the newly combined array. The second output confirms that the original numbers array remains unchanged.

If you want to store the combined result back in the same variable, you can reassign it:

let numbers = [1, 2, 3];

numbers = numbers.concat([4, 5, 6]);

console.log(numbers);

Output:

[1, 2, 3, 4, 5, 6]

Although the variable now refers to the combined array, concat() itself still returns a new array rather than modifying the original one.

Understanding Shallow Copy Behavior

The concat() method creates a shallow copy of the elements it combines. This means that primitive values are copied as values, but nested arrays and objects are copied as references.

Consider the following example:

const original = [
  10,
  20,
  { name: "JavaScript" }
];

const copied = original.concat();

copied[0] = 100;
copied[2].name = "TypeScript";

console.log(original);
console.log(copied);

Output:

[10, 20, { name: "TypeScript" }]

[100, 20, { name: "TypeScript" }]

The primitive value at index 0 changes only in the copied array. However, the object at index 2 is shared by both arrays. Therefore, changing its property through copied is also visible through original.

The same principle applies to nested arrays:

const first = [[1, 2], [3, 4]];
const second = first.concat();

second[0].push(5);

console.log(first);
console.log(second);

Output:

[[1, 2, 5], [3, 4]]
[[1, 2, 5], [3, 4]]

Both arrays contain a reference to the same nested array at index 0. If you need a deep copy, use an appropriate deep-cloning approach rather than relying on concat().

Using concat() with Array-Like Objects

By default, concat() treats ordinary objects as individual values rather than automatically combining their properties or indexed elements.

const numbers = [10, 20];

const result = numbers.concat({
  0: 30,
  1: 40,
  length: 2
});

console.log(result);

Output:

[
  10,
  20,
  { 0: 30, 1: 40, length: 2 }
]

However, JavaScript provides the Symbol.isConcatSpreadable property to control whether an object should be spread into the resulting array.

const numbers = [10, 20];

const values = {
  0: 30,
  1: 40,
  length: 2,
  [Symbol.isConcatSpreadable]: true
};

const result = numbers.concat(values);

console.log(result);

Output:

[10, 20, 30, 40]

Setting Symbol.isConcatSpreadable to true tells concat() to treat the object as spreadable. Its indexed elements are added individually.

You can also make an array non-spreadable:

const first = [1, 2];
const second = [3, 4];

second[Symbol.isConcatSpreadable] = false;

const result = first.concat(second);

console.log(result);

Output:

[1, 2, [3, 4]]

This feature provides additional control over how values are combined.

Difference Between concat() and Spread Syntax

Modern JavaScript also provides spread syntax for combining arrays. It offers a concise alternative to concat().

Using concat():

const first = [1, 2, 3];
const second = [4, 5, 6];

const result = first.concat(second);

console.log(result);

Using spread syntax:

const first = [1, 2, 3];
const second = [4, 5, 6];

const result = [...first, ...second];

console.log(result);

Output for both examples:

[1, 2, 3, 4, 5, 6]

Both approaches create a new array containing the elements of the supplied arrays. Spread syntax is often easier to read when combining arrays with additional values.

For example:

const first = ["HTML", "CSS"];
const second = ["React", "Angular"];

const result = ["JavaScript", ...first, ...second, "Node.js"];

console.log(result);

Output:

["JavaScript", "HTML", "CSS", "React", "Angular", "Node.js"]

Use whichever approach makes the intention of your code clearer. Remember that spread syntax also performs a shallow copy.