Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this forEach return undefined when using a return statement

Tags:

javascript

Object.prototype.e = function() {
    [].forEach.call(this, function(e) {
        return e;
    });
}; 
var w = [1,2];

w.e(); // undefined

But this works if I use alert instead

// ...
[].forEach.call(this, function(e) {
    alert(e);
});
// ...

w.e(); // 1, 2
like image 661
David G Avatar asked Aug 26 '11 19:08

David G


People also ask

Why is forEach returning undefined?

The "Cannot read property 'forEach' of undefined" error occurs when calling the forEach method on an undefined value. To solve the error make sure to initialize the variable to the correct value and only call the forEach method on the correct data type.

What happens if you return in a forEach?

Using return in a forEach() is equivalent to a continue in a conventional loop.

Does return stop a forEach?

'return' doesn't stop looping The reason is that we are passing a callback function in our forEach function, which behaves just like a normal function and is applied to each element no matter if we return from one i.e. when element is 2 in our case.

Why is my array returning undefined?

The array the map() method returns consists of the values that are returned from the callback function. If you don't return a value from a function in JavaScript, you implicitly return undefined . Copied! This is the most common reason the map() method returns an array containing undefined values.


1 Answers

Your example is a bit odd, but as this question is becoming the canonical "return from forEach" question, let's use something simpler to demonstrate the problem:

Here, we have a function that checks the entries in an array to see if someProp matches value and, if so, increments the count on the entry and returns the entry:

function updateAndReturnMatch(array, value) {
    array.forEach(function(entry) {
        if (entry.someProp == value) {
           ++entry.count;
           return entry;
        }
    });
}

But calling updateAndReturnMatch gives us undefined, even if the entry was found and updated.

The reason is that the return inside the forEach callback returns from the callback, not from updateAndReturnMatch. Remember, the callback is a function; return in a function returns from that function, not the one containing it.

To return from updateAndReturnMatch, we need to remember the entry and break the loop. Since you can't break a forEach loop, we'll use some instead:

function updateAndReturnMatch(array, value) {
    var foundEntry;
    array.some(function(entry) {
        if (entry.someProp == value) {
           foundEntry = entry;
           ++foundEntry.count;
           return true; // <== Breaks out of the `some` loop
        }
    });
    return foundEntry;
}

The return true returns from our some callback, and the return foundEntry returns from updateAndReturnMatch.

Sometimes that's what you want, but often the pattern above can be replaced with Array#find, which is new in ES2015 but can be shimmed for older browsers:

function updateAndReturnMatch(array, value) {
    var foundEntry = array.find(function(entry) {
        return entry.someProp == value;
    });
    if (foundEntry) {
        ++foundEntry.count;
    }
    return foundEntry;
}
like image 132
T.J. Crowder Avatar answered Sep 23 '22 00:09

T.J. Crowder