Page 104 - JavaScript
P. 104
Results in a new Array:
["a", "b", "c", "d", "e", "f", "g", "h"]
Without Copying the First Array
var longArray = [1, 2, 3, 4, 5, 6, 7, 8],
shortArray = [9, 10];
3
Provide the elements of shortArray as parameters to push using Function.prototype.apply
longArray.push.apply(longArray, shortArray);
6
Use the spread operator to pass the elements of shortArray as separate arguments to push
longArray.push(...shortArray)
The value of longArray is now:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Note that if the second array is too long (>100,000 entries), you may get a stack overflow error
(because of how apply works). To be safe, you can iterate instead:
shortArray.forEach(function (elem) {
longArray.push(elem);
});
Array and non-array values
var array = ["a", "b"];
3
var arrConc = array.concat("c", "d");
6
var arrConc = [...array, "c", "d"]
Results in a new Array:
["a", "b", "c", "d"]
You can also mix arrays with non-arrays
https://riptutorial.com/ 61

