JavaScript ES6 – Difference between find, filter and some methods
Published:
Jan 10, 2018
3 mins
read
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 returnundefined
filter()
will return all matching elements, if no match found it will return[]
empty objectsome()
will returntrue
if any match is found, if no match found it will returnfalse
most used JavaScript array methods
Want to know the most common JavaScript array methods? See the list of methods below or read most common array method with examples.