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

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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>GeeksforGeeks1.</li>
            <li>GeeksforGeeks2.</li>
            <ol>
                <li>GeeksforGeeks3.</li>
                <li>GeeksforGeeks4.</li>
                <ul>
                    <li>GeeksforGeeks5.</li>
                    <li>GeeksforGeeks6.</li>
                </ul>
            </ol>
        </ul>
    </body>
      
    </html>

    chevron_right

    
    

  • Output:
  • Example 2: Changes made when we use the filter() method for searching.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>GeeksforGeeks1.</li>
            <li>GeeksforGeeks2.</li>
            <ol>
                <li>GeeksforGeeks3.</li>
                <li>GeeksforGeeks4.</li>
                <ul>
                    <li>GeeksforGeeks5.</li>
                    <li>GeeksforGeeks6.</li>
                </ul>
            </ol>
        </ul>
    </body>
      
    </html>

    chevron_right

    
    

  • Output:

full-stack-img




My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.