filter an array with another array

Filtering an array with another array is useful when there are many elements that need to be removed.

In the code example below, we want to keep clothes that are shirts or sweaters.

let clothes = ['shirt', 'sweater', 'sweater', 'shirt', 'pants', 'pants', 'underwear'];
const clothesFilters = ['shirt', 'sweater'];

// filter removes an element if the returned value is false
// otherwise it will keep it
clothes = clothes.filter(function (element) {
  // includes checks if the element exist on the array it was called on
  return clothesFilters.includes(element);
});

console.log(clothes); // ["shirt", "sweater", "sweater", "shirt"]
javascript
function declarations in JavaScript

In JavaScript functions can be declared with function statements, anonymous functions and arrow functions.

javascript
changing the type of a value - coercion in javascript

Using a value in a comparison can be confusing sometimes because JavaScript tries to be helpful and implicitly change the type for you.