What is the difference between jQuery.size() and jQuery.length?

jQuery.size() method returns the number of elements in the jQuery object. The size() method was deprecated in jQuery 1.8 and completely removed in jQuery 3.0. As an alternative, the length property is used.

Example

You can try to run the following code to learn how to work with size() method ?
Note: To run the jQuery size() method, use jQuery version less than 1.8, since it was deprecated in jQuery 1.8. Generally, the length property is preferred now.

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $(".btn").click(function(){
                alert($("li").size());
            });
        });
    </script>
</head>
<body>
    <button class="btn">How many li elements?</button>
    
    <ol>
        <li>India</li>
        <li>US</li>
        <li>UK</li>
        <li>Australia</li>
    </ol>
</body>
</html>

The output of the above code is ?

4

jQuery length Property

The length property shows the number of elements in the jQuery object. length does the same work as the size() method but avoids the overhead of a function call, making it more efficient.

Example

You can try to run the following code to learn how to work with jQuery length property ?

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $(".btn").click(function(){
                alert($("li").length);
            });
        });
    </script>
</head>
<body>
    <button class="btn">How many li elements?</button>
    
    <ol>
        <li>India</li>
        <li>US</li>
        <li>UK</li>
        <li>Australia</li>
    </ol>
</body>
</html>

The output of the above code is ?

4

Conclusion

Both jQuery.size() and jQuery.length return the count of elements, but length is preferred as it's a property rather than a method, making it faster and compatible with modern jQuery versions.

Updated on: 2026-03-13T19:25:21+05:30

594 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements