JavaScript Array concat() Method

The JavaScript Array.concat() method is used to merge two or more arrays. Unlike modifying the existing arrays, the concat() method creates a new array that combines the elements of the original arrays. This method can also be used to concatenate values (such as numbers or strings) into the resulting array.

For example, array1.concat(array2) will return a new array that includes all elements from both array1 and array2, without altering the original arrays. This method has been part of the JavaScript language for a long time and is supported in all modern browsers.

Syntax

The syntax for the Array.concat() method is given below:

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

Parameters

The Array.concat() method accepts one or more parameters:

  • value1, value2, …, valueN (Optional): These are the arrays or values to be concatenated into the new array. The method can accept any number of arrays or values.

Return Value

A new array containing the elements of the original array(s) followed by the elements or values specified in the argument(s). The original array remains unchanged.

Examples of JavaScript Array.concat() Method

Example 1: Concatenating Two Arrays

You can use the Array.concat() method to merge two arrays into a single array.

javascript
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const result = arr1.concat(arr2);
console.log(result);
console.log(arr1);
console.log(arr2);

Explanation: In this example, arr1 and arr2 are merged into a new array result. The original arrays arr1 and arr2 remain unchanged.

Example 2: Concatenating Arrays and Values

You can concatenate arrays along with individual values using the concat() method.

javascript
const arr1 = [10, 20];
const result = arr1.concat(30, 40, [50, 60]);
console.log(result);

Explanation: The concat() method combines both individual values (30, 40) and another array [50, 60] into the original array arr1 to form the new array result.

Example 3: Concatenating More Than Two Arrays

You can concatenate multiple arrays by passing them as arguments to the concat() method.

javascript
const arr1 = ['a', 'b'];
const arr2 = ['c', 'd'];
const arr3 = ['e', 'f'];
const result = arr1.concat(arr2, arr3);
console.log(result);

Explanation: In this example, three arrays (arr1, arr2, and arr3) are concatenated into a new array result.

Example 4: Concatenating Nested Arrays

The concat() method does not flatten nested arrays. If you pass an array within another array, the nested array will be included as-is.

javascript
const arr1 = [1, 2];
const arr2 = [3, [4, 5]];
const result = arr1.concat(arr2);
console.log(result);

Explanation: The nested array [4, 5] is not flattened, and it remains as an array within the resulting array.

Example 5: Concatenating with Empty Arrays

If you concatenate an empty array, the result will be the same as the original non-empty array.

javascript
const arr1 = [1, 2, 3];
const emptyArr = [];
const result = arr1.concat(emptyArr);
console.log(result);  // Output: [1, 2, 3]

Explanation: Since emptyArr contains no elements, concatenating it with arr1 results in an array identical to arr1.

JavaScript

2029

876

Related Articles