Difference between find() and filter() in JavaScript | ES6 some() and every() functions example

Many time we need to traverse the array data while building the application. Fortunately, JavaScript provides many array functions to do that but use case of these functions are different. Here in the below description, we'll discuss the ES6 array functions find() and filter().

If we only wanted to return the first matching element in the array: We use find() for this. .find() will look and stop after the first match. Let me explain with an example:

Let say we have a products array and we wanted to find the price of TV in below example:

The output of the above script will be:

Now If we run filter() function in the same case then it will return the array of objects whose name is TV. .filter() will continue searching through the entire array.

The output of the above script will be:

find() and filter() are only used when we wanted to return the matching results and use them but if we just wanted to check the existence of matching result, in that case, we can use some() and every() ES6 functions...  Read More

Share This: