is there null/undefined objects coming from forEach?

Viewed 32

I'm experiencing some behaviour that I can't explain. Here is the javascript code to copy items from a source array to a destination array in 3 different ways. The first 2 run successfully, the third one doesn't.

var source = [];
source.push({id: 1, name: 'X'});
source.push({id: 2, name: 'Y'});

var dest = [];
source.forEach(i => dest.push(i));
console.log('dest 1 success');

source.forEach(pushToDest);
console.log('dest 2 success');

source.forEach(dest.push);
console.log('dest 3 success');


function pushToDest(i) {
    dest.push(i);
}

the jsfiddel can be seen at: https://jsfiddle.net/qhb6oaye/

the third way to copy the items into the new array gives following error:

Uncaught TypeError: Cannot convert undefined or null to object"

is there any hidden null/undefined objects in the forEach stream? But then why does Version 2 work? I'm confused and need help to understand this.

0 Answers
Related