Array Methods

Concatenating Arrays

🙋 Need help? Ask an expert now!

Concatenating Arrays is another useful array method that can be used for many different purposes. The syntax of the method looks like this: arr1.concat(arr2). The concat() methods inserts the element of arr2 (the array inside the brackets) to the end of arr 1 (the first array). This method returns a new array as a result of adding the two arrays. The two arrays used in the concatenation remain unchanged after the method is called.

As shown above, the arrays arr 1 and arr 2 are concatenated to produce arr 3. The resulting array arr 3 has both the elements of the first array as well as the second array. This method will prove to be useful when you want to split a big array into different smaller arrays for specific calculations before making them a big array again. The arrayname.concat() method is similar to the concat() method used when adding two strings together.

Edit Me on GitHub!

Application Question

Consider the following code segment:

var arrOne = [1, 3, 5, 7, 9];
            var arrTwo = [2, 4, 6, 8, 10];
            var arrThree = arrOne.concat(arrTwo);

What will the following code segment print to the console?