DEV Community

Discussion on: forEach( ), sort( ) & filter( ) for newbies

Collapse
 
romangraef profile image
Roman Gräf

Hey, just wanted to ask: Shouldn't the callback of .filter() return a truthy/falsey value? I guess it works in this case, but it could be confusing if someone tried to use filter in the sense of how .map() is supposed to be used. Also this probably fails if yummies contained a falsey value like an empty string which it wanted to keep.

Collapse
 
kauresss profile image
Kauress

Hi,the students for whom this was written only know some basic array methods and haven't done map() as yet. So I wrote this article around what they already know, as beginners getting into advanced JS. The filter() method iterates over each element in the array and passes it to the filterYums function. Yes, if the condition yummies.indexOf(currentItem) === index is met it will return true and that element will be passed to the new array. The condition does not check for falsey values like empty strings, null and undefined.