JavaScript Concat Array

JavaScript Concat Array | Arrays are the most basic and important data type in any programming language. It has an end number of applications and use cases. One can create and use arrays in JavaScript as well. The concat() method combines (concatenates) two or more arrays, returns a new array containing the concatenated fields, and does not modify an existing array.

Along with creating and using the array to store data, JavaScript provides various types of in-built methods which can be used to perform operations on an array. In this article, we will talk about the concat() JavaScript array method. The word concat is derived from concatenation which means linking something with each other. 

Let us have a look at the syntax.

Syntax:
array1.concat(array2, array3, …, arrayX)

The concat() method is used in JavaScript to merge two or more arrays. To use this method we need to call it on the first array and pass the other arrays as an argument. This means that the method returns a new array with contents starting from array one to array two.

JavaScript Concat Two Array

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

Output:-

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

A new array is created using the concat() method. The items of the entity that is being executed will first fill the array. The significance of every statement then becomes concatenated into the array; for normal objects or elements, the statement on its own becomes an element of the finished array; for arrays or array-like objects, also every component of the argument would be individually incorporated into the final array if the property Symbol.isConcatSpreadable() is assigned to a factual value. The hierarchical array arguments are not recursively explored by the concat() function.

The concat process gives a basic version that includes copies of the exact elements merged from the original arrays rather than altering this or any other array given as inputs. Components of the old arrays are copied as shown below:-

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

arr1[2] = 8;
arr2[2] = 9;
console.log(arr1);
console.log(arr2);
console.log(arr3);

Output:-

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

Concat Arrays JavaScript

Let us see a JavaScript program to multiple arrays into a single array.

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let arr3 = [7, 8];
let arr4 = [9, 10];
let arr5 = arr1.concat(arr2, arr3, arr4);
console.log(arr5);

Output:-

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

Concat copies object references (rather than the actual item) into the new array. The same object is referred to by both the original and new arrays. That is, both the new and old arrays can see modifications made to a referred item. This includes parameters for arrays that have elements that are arrays as well.

Concatenation duplicates the values of data types like strings, integers, and booleans (but not String, Number, and Boolean objects) into the new array.

The original array(s) or value(s) won’t be affected by concatenation. Additionally, no operation on the new array will have an impact on the original arrays, with the exception of operations on elements that are object references.

JavaScript Concat Arrays of Different Types

We can also concatenate different types of arrays. Have a look at the example below:

const letters = [1, 2, 3];
const numbers = ['a', 'b', 'c'];

const alphaNumeric = letters.concat(numbers);
console.log("Concatenated array is: " + alphaNumeric);
console.log(typeof alphaNumeric);

Output:-

Concatenated array is: 1,2,3,a,b,c
object

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *