What is the difference between find() and filter() methods in JavaScript ?

04 Apr, 2022
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 leaf.
The filter() method is used to filters 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 search through all the elements while find() method search through all the child elements only.
Example 1: This example uses the find() method to search the element.<!DOCTYPE html><html>  <head>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">    </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>GeeksforGeeks1.</li>        <li>GeeksforGeeks2.</li>        <ol>            <li>GeeksforGeeks3.</li>            <li>GeeksforGeeks4.</li>            <ul>                <li>GeeksforGeeks5.</li>                <li>GeeksforGeeks6.</li>            </ul>        </ol>    </ul></body>  </html>
<!DOCTYPE html><html>  <head>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">    </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>GeeksforGeeks1.</li>        <li>GeeksforGeeks2.</li>        <ol>            <li>GeeksforGeeks3.</li>            <li>GeeksforGeeks4.</li>            <ul>                <li>GeeksforGeeks5.</li>                <li>GeeksforGeeks6.</li>            </ul>        </ol>    </ul></body>  </html>
Output:
Example 2: Changes made when we use the filter() method for searching.<!DOCTYPE html><html>  <head>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">    </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>GeeksforGeeks1.</li>        <li>GeeksforGeeks2.</li>        <ol>            <li>GeeksforGeeks3.</li>            <li>GeeksforGeeks4.</li>            <ul>                <li>GeeksforGeeks5.</li>                <li>GeeksforGeeks6.</li>            </ul>        </ol>    </ul></body>  </html>
<!DOCTYPE html><html>  <head>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">    </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>GeeksforGeeks1.</li>        <li>GeeksforGeeks2.</li>        <ol>            <li>GeeksforGeeks3.</li>            <li>GeeksforGeeks4.</li>            <ul>                <li>GeeksforGeeks5.</li>                <li>GeeksforGeeks6.</li>            </ul>        </ol>    </ul></body>  </html>
Output:
Let us see the Differences in Tabular Form -:
Its syntax is -:
array.find(function(value, Index, array),thisValue)
Its syntax is -:
array.filter(function(value, Index, array), thisValue)
mayank007rawa
HTML-Misc
JavaScript-Misc
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
Roadmap to Learn JavaScript For Beginners
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?