DEV Community

Zaryab Khalid
Zaryab Khalid

Posted on

Difference between find() and filter() methods in Javascript ?

find() Method

This method is used to find the single item in the array. find() method return a single item that fulfill the condition which passes into the callback function.

let array = ['apple','orange','WaterMelon','Melon']   

//apply find() method to the array

console.log(array.find((item) => item=='apple'))
returns --> apple
Enter fullscreen mode Exit fullscreen mode

filter() Method

This method is used to find the multiple items in the array. filter() method return all those items which fulfills the condition that passes into the callback function.This method return a new array without modifying the original array.

let array = [1,2,3,4,5,6,7,8,9]   
//apply filter() method to the array
console.log(array.filter((item) => item >=4))`
returns --> new Array [4,5,6,7,8,9]
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
amitk1990 profile image
Amit • Edited

difference is one returns first value and other returns an array.
example

[1,2,3,4,4].find(item => item === 4);
4
[1,2,3,4,4].filter(item => item === 4);
[4, 4]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
zaryabkhalid profile image
Zaryab Khalid

great