I tried to merge two arrays in JavaScript. I wrote a code like this:
var array1 = [1, 2];
var array2 = [3, 4];
array2.forEach(function(value) {
array1.push(value);
});
I know there are better ways such as array1 = array1.concat(array2);
and this question is not about it.
I tried to minimize the above code like this:
var array1 = [1, 2];
var array2 = [3, 4];
array2.forEach(array1.push);
It is same code but I do not create the extra unnamed function. I pass array1.push function directly to forEach. I expected same result but I get this exception:
Cannot convert undefined or null to object
I do not understand why I am getting this exception?