8 3 1 common array methods 2f 2f concat examples

Solutions on MaxInterview for 8 3 1 common array methods 2f 2f concat examples by the best coders in the world

showing results for - "8 3 1 common array methods 2f 2f concat examples"
Sofie
26 Sep 2017
1//The general syntax for this method is:
2arrayName.concat(otherArray1, otherArray2, ...)
3
4/*This method adds the elements of one array to the end of another. 
5The new array must be stored in a variable or printed to the screen, 
6because concat does NOT alter the original arrays.*/
7
8let arr = [1, 2, 3];
9let otherArray = ['M', 'F', 'E'];
10let newArray = [];
11
12newArray = arr.concat(otherArray);
13console.log(newArray);
14
15newArray = otherArray.concat(arr);
16console.log(newArray);
17
18console.log(arr.concat(otherArray, arr));
19
20console.log(arr);
21
22//[1, 2, 3, 'M', 'F', 'E']
23//[ 'M', 'F', 'E', 1, 2, 3 ]
24//[ 1, 2, 3, 'M', 'F', 'E', 1, 2, 3 ]
25//[1, 2, 3]