Javascript method arr.includes()


#1

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/web-dev-intermediate-javascript-practice/lessons/intermediate-javascript-coding-challenge/exercises/just-cool-stuff
I got stuck here.
image
We use this function to return an array that are present in both arrays.
image
I can’t understand how it works here. Doesn’t arr.includes a boolean method? so I think secondArray.includes(item) will return either true or false. And the result will be passed to .filter method. so finally we get true, false, false, true, true… either true or false.


#2

If you replace filter with map, then that is the behaviour you would get. But filter’s behaviour is not the same as map’s.


#3

I mean finally I want it to return an array, but within filter method there is includes method which will return either true or false. then how can you filter true and false to get the array? I experimented with it. the code worked well and return array as expected . But I am confused how it works.


#4

It doesn’t filter true/false. It asks your predicate whether something should be included, and based on the response it either includes that something, or not. And it’s not any different from map in that regard, it has the same pattern of consulting a function about each value in the array.

Say you have an array. It’s named Bob.
Bob! Show me all your values! Bob does. Arrays can do this.
Bob! Show me all your values for which this function returns true! Bob can do this too, it’s no different.

You may want to implement a filter function yourself to get yourself to think through it.