In this article, we will see the difference between the find() and filter() methods in javascript.
JavaScript find() method: The find() method is used to find all the descendant elements of the selected element. It finds the element in the DOM tree by traversing through the root to the leaf.
Syntax:
array.find(function(currentValue, index, arr),thisValue);
Example 1: This example uses the find() method to search the element.
HTML
<!DOCTYPE html> < html > < head > < script src = </ script > < script > $(document).ready(function() { $("ul").find(":odd") .css("background-color", "yellow"); }); </ script > </ head > < body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h4 >Find() Method</ h4 > < ul > < li >neveropen1.</ li > < li >neveropen2.</ li > < ol > < li >neveropen3.</ li > < li >neveropen4.</ li > < ul > < li >neveropen5.</ li > < li >neveropen6.</ li > </ ul > </ ol > </ ul > </ body > </ html > |
Output:
JavaScript filter() method: The filter() method is used to filter all the elements and returns the element that matches and the element that do not match are removed. The only difference is the filter() method searches through all the elements while find() method searches through all the child elements only.
Syntax:
array.filter(callback(element, index, arr), thisValue)
Example 2: Changes made when we use the filter() method for searching.
HTML
<!DOCTYPE html> < html > < head > < script src = </ script > < script > $(document).ready(function() { $("ul").filter(":odd"). css("background-color", "yellow"); }); </ script > </ head > < body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h4 >Find() Method</ h4 > < ul > < li >neveropen1.</ li > < li >neveropen2.</ li > < ol > < li >neveropen3.</ li > < li >neveropen4.</ li > < ul > < li >neveropen5.</ li > < li >neveropen6.</ li > </ ul > </ ol > </ ul > </ body > </ html > |
Output:
Let us see the Differences in Tabular Form:
find() |
filter() |
|
---|---|---|
1. | The find() method is used to find all the descendant elements of the selected element. | The filter() method is used to filter all the elements |
2. | The find() method finds the element in the DOM tree by traversing through the root to the leaf. | The filter() method returns the element that matches and removes the element that does not match. |
3. | The find() method searches through all the child elements only. | The filter() method searches through all the elements |
4. | It does not execute the function for empty elements. | The filter() method does not change the original array. |
5. | It does not change the original array. | The filter() method does not execute the function for empty elements. |
6. |
Its syntax is -: array.find(function(value, Index, array),thisValue) |
Its syntax is -: array.filter(function(value, Index, array), thisValue) |
7. | This method returns undefined if no elements are found. | In filter() method a value is passed to the function as this value |