concat
ExamplesΒΆThe general syntax for this method is:
arrayName.concat(otherArray1, otherArray2, ...)
This method adds the elements of one array to the end of another. The new array
must be stored in a variable or printed to the screen, because concat
does
NOT alter the original arrays.
Example
1let arr = [1, 2, 3];
2let otherArray = ['M', 'F', 'E'];
3let newArray = [];
4
5newArray = arr.concat(otherArray);
6console.log(newArray);
7
8newArray = otherArray.concat(arr);
9console.log(newArray);
10
11console.log(arr.concat(otherArray, arr));
12
13console.log(arr);
Output
[1, 2, 3, 'M', 'F', 'E']
[ 'M', 'F', 'E', 1, 2, 3 ]
[ 1, 2, 3, 'M', 'F', 'E', 1, 2, 3 ]
[1, 2, 3]