JavaScript ES6 – Difference between find, filter and some methods

JavaScript find(), filter() and some() methods are really similar and same time different on how we use them.

Every method behaves differently as each will return different result for testing an array.

Let’s demonstrate the difference between each with simple example:

array find() method

Returns the first matching element in the array.

        
          let arr = [9, 4, 24, 6];
const result = arr.find((value) => value % 2 === 0);
console.log(result);

// OUTPUT
// 4        
      

array filter() method

Returns the all matching element in the array.

        
          let arr = [9, 4, 24, 6];
const result = arr.filter((value) => value % 2 === 0);
console.log(result);

// OUTPUT
// [4, 24, 6]        
      

array some() method

Returns true if any matching found in the array.

        
          let arr = [9, 4, 24, 6];
const result = arr.some((value) => value % 2 === 0);
console.log(result);

// OUTPUT
// true        
      

what are the different between find(), filter() and some() methods?

Here the different between all 3 methods:

  • find() will only return the first matching elements, if no match found it will return undefined
  • filter() will return all matching elements, if no match found it will return [] empty object
  • some() will return true if any match is found, if no match found it will return false
%d bloggers like this: