How does `item` work in this?

How does item work in this? Here is a link to the challenge justCoolStuff()

const justCoolStuff = (arr1, arr2) => arr1.filter(item => arr2.includes(item));

const coolStuff = ['gameboys', 'skateboards', 'backwards hats', 'fruit-by-the-foot', 'pogs', 'my room', 'temporary tattoos'];

const myStuff = [ 'rules', 'fruit-by-the-foot', 'wedgies', 'sweaters', 'skateboards', 'family-night', 'my room', 'braces', 'the information superhighway']; 

console.log(justCoolStuff(myStuff, coolStuff))
// Should print [ 'fruit-by-the-foot', 'skateboards', 'my room' ]

item is a parameter for the .filter() function. Basically, the .filter() function loops through the arr1 array, then checks to see if each item is in arr2. You could also do it using conventional loops.

let arr1 = ["a", "b", "c"];
let arr2 = ["a", "z", "d"];
const joinArrs = (arr1, arr2) => {
let arr2AndArr1 = [];
for (let i = 0; i<arr1.length; i++){
  if (arr2.includes(arr1[i])){
console.log(joinArrs(arr1, arr2));

This would do the same job as:

let arr1 = ["a", "b", "c"];
let arr2 = ["a", "z", "d"];
let arr2AndArr1 = (ar1, ar2) => ar1.filter(item=>ar2.includes(item));
console.log(arr2AndArr1(arr1, arr2));

The item acts like the arr1[i] from the first example.

1 Like