.map and .filter methods confusion with return statement javascript

Hi Guys,
I am a bit struggling with the return for the below two scenarios, using .map and .filter functions.
why the return statement when using the .filter methods return the items in the items array rather then bollian (true or false) as the return is ( item.split(’’).reverse().join(’’) === item;) which if i return correctly, says it will return the comparision between two items.
really appreciate if someone can clarify me the below concept.

////////////using Map///////////////
var items = ['mum', 'dad', 'brother'];
items.map(function(item) {
  return item.split('').reverse().join('') === item;
});

// returns [true, true, false]
///////////////////////////////////////
//////////////using Filter////////////////
var items = ['mum', 'dad', 'brother'];
items.filter(function(item) {
  return item.split('').reverse().join('') === item;
});

// returns ['mum', dad']
//////////////////////////////////////////////

That’s what filter does, it returns the original items based on the return value inside the callback. If the callback returns true for that iteration in the looping, the item is kept in the result. If it returns false, the item is removed from the result - the result that represents a “filtered” version of the original array - the same values, just some may have been removed.

map on the other hand changes values. Same number of values, but the values change depending on the return values of the callback function. Map doesn’t remove values like filter does, only changes them. Filter doesn’t change values like map does, only removes them.

2 Likes

This could be part of a nursery rhyme for kids! Sounds similar to Goosey Gander.

Ok, I’ll leave now.

:stuck_out_tongue: