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.

Let say we just wanted to check that whether product whose name is TV existence in products array or not? In this case, we can use ES6 some():

Here result will be true because product TV exists in the products array.

In some cases, we just wanted to check that every object should have a matching element in that case we use JavaScript ES6 every().

Let say we wanted to check whether all product price is greater than 1199 or not?

This will return true because every product's price is greater than 1199.

Now if I wanted to check whether every product's price is greater that 1200.

In this case, the result will be false because Bowl price is not greater than 1200

Share This:

One thought on “Difference between find() and filter() in JavaScript | ES6 some() and every() functions example

Leave a Reply

Your email address will not be published. Required fields are marked *