jQuery .size() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Legacy API

What You’ll Learn

The .size() method returns how many elements are in a jQuery collection. This tutorial covers the official demos, equivalence with .length, empty-set behavior, why the method was removed in jQuery 3.0, how to migrate, five examples, and try-it labs (legacy jQuery for .size(), modern jQuery for .length).

01

.size()

Count matches

02

.length

Preferred

03

Integer

Return type

04

Empty

Returns 0

05

Deprecated

Since 1.8

06

Removed

jQuery 3.0

Introduction

After you select elements with jQuery, you often need a simple question answered: how many did I match? Historically that was $("li").size(). Today the answer is always $("li").length.

.size() was added in jQuery 1.0, marked deprecated in 1.8, and removed in 3.0. It never accepted arguments and always returned the same integer as the .length property — with the extra cost of a function call.

This page keeps the official API examples so you can read legacy code and migrate confidently. New projects should use .length only. Explore more DOM topics in the jQuery DOM hub.

Understanding the .size() Method

.size() returns an Integer: the number of elements in the current jQuery object. It does not walk children automatically — it counts whatever is already in the matched set (after your selector, .filter(), .find(), and so on).

The official docs stress that .size() and .length are functionally equivalent, but .length is preferred because it skips the method-call overhead.

💡
Beginner Tip

If you see .size() in an old tutorial, replace it with .length and you are done. Same number, no parentheses, works on jQuery 3+.

⚠️
Legacy warning

Live demos that call .size() must load jQuery 1.x or 2.x. On jQuery 3+, .size is undefined and calling it throws.

📝 Syntax

Signature (jQuery 1.0–2.x; removed in 3.0):

jQuery
.size()

Parameters

  • None — this method does not accept any arguments.

Return value

  • Integer — count of elements in the jQuery collection (same as .length).

Modern replacement

jQuery
// Legacy (1.x / 2.x only)
var n = $( "li" ).size();

// Preferred (all jQuery versions, including 3+)
var n = $( "li" ).length;

⚡ Quick Reference

GoalCode
Count matches (legacy)$("div").size()
Count matches (modern)$("div").length
Empty selection$("#none").length // 0
After filter$("li").filter(".active").length
Guard before workif ($(".item").length) { … }
.size()
legacy

Method call; deprecated 1.8; gone in 3.0

.length
preferred

Property read; same integer, no call overhead

Returns
Integer

How many elements are in the jQuery object

Empty set
0

Safe to use in if checks and UI counters

📋 .size() vs .length

.size().length
TypeMethodProperty
ResultInteger countInteger count (same)
jQuery 1.x / 2.xAvailableAvailable
jQuery 3+RemovedAvailable
RecommendationLegacy onlyAlways prefer

Examples Gallery

Examples follow the official jQuery API and common migration patterns. Labs 1–4 use jQuery 1.12.4 so .size() still exists; lab 5 uses jQuery 3.7.1 with .length.

📚 Getting Started

Official-style demos that show .size() matching .length.

Example 1 — Official Pattern: Count List Items

Given a short unordered list, both .size() and .length report how many <li> elements matched.

jQuery
alert( "Size: " + $( "li" ).size() );
alert( "Size: " + $( "li" ).length );
Try It Yourself

How It Works

$("li") builds a jQuery object containing every list item. .size() returns that object’s count; .length reads the same value as a property. Prefer .length in new code.

Example 2 — Official Pattern: Count Divs on Click

Each click appends a new <div>, then updates a message with the current count via .size().

jQuery
$( document.body )
  .on( "click", function() {
    $( this ).append( $( "<div>" ) );
    var n = $( "div" ).size();
    $( "span" ).text( "There are " + n + " divs. Click to add more." );
  })
  // Trigger the click to start
  .trigger( "click" );
Try It Yourself

How It Works

After appending, $("div").size() rescans the document for all div elements and returns the new total. Replace .size() with .length for the same live counter on jQuery 3+.

📈 Practical Patterns

Empty sets, filters, and migrating off the removed API.

Example 3 — Empty Selection Returns 0

Missing IDs and empty filters are safe: the count is 0, not an error.

jQuery
var missing = $( "#does-not-exist" ).size(); // 0
var empty = $( ".nope" ).size();              // 0

if ( $( ".card" ).size() === 0 ) {
  $( "#status" ).text( "No cards yet" );
}
Try It Yourself

How It Works

An empty jQuery object still has a length of zero. Checking the count before updating the UI avoids acting on selections that matched nothing.

Example 4 — Count After Filtering

.size() (and .length) reflect the current set — after .filter() or .find(), the count shrinks.

jQuery
var all = $( "li" ).size();
var active = $( "li" ).filter( ".active" ).size();
$( "#out" ).text( "all=" + all + ", active=" + active );
Try It Yourself

How It Works

.filter(".active") returns a new jQuery object with only matching items. Calling .size() on that object counts the filtered subset, not the original list.

Example 5 — Migrate to .length on jQuery 3+

On modern jQuery, use .length. Calling .size() fails because the method no longer exists.

jQuery
// jQuery 3.7.1
var n = $( "li" ).length;          // works
typeof $( "li" ).size;             // "undefined"
// $( "li" ).size();               // TypeError — do not call
Try It Yourself

How It Works

jQuery 3 removed the method. Search your codebase for .size() and replace with .length before upgrading. The try-it lab loads jQuery 3.7.1 so you can see the difference live.

🚀 Common Use Cases

  • UI counters — “3 items selected” badges (use .length today).
  • Empty-state checks — show a placeholder when .length === 0.
  • Validation — require at least one checked checkbox before submit.
  • Reading legacy code — understand old books/blogs that still show .size().
  • Migration audits — find-and-replace .size() before a jQuery 3 upgrade.
  • Filtered tallies — count only .active / :visible matches after narrowing.

🧠 How Counting Works

1

Select elements

Build a jQuery object with a selector or chain (.filter, .find).

Select
2

Ask for the count

Legacy: call .size(). Modern: read .length.

Count
3

Get an integer

Use the number in messages, conditions, or loops.

Integer
4

Prefer .length

Same answer, zero migration risk on jQuery 3+.

📝 Notes

  • Added in jQuery 1.0; deprecated in 1.8; removed in 3.0.
  • Functionally identical to the .length property.
  • .length is preferred — no function-call overhead.
  • Counts the matched set only, not every descendant unless selected.
  • Empty selections return 0.
  • On jQuery 3+, .size is undefined — calling it throws.
  • Try-it labs 1–4 intentionally load jQuery 1.12.4 so .size() still works.

Browser Support

.size() existed from jQuery 1.0 through 2.x. It was deprecated in 1.8 and removed in 3.0. Use .length for jQuery 3.x / 4.x and for any new code. The property works in every browser your jQuery build supports.

Legacy 1.0–2.x

jQuery .size()

Historical counting API. Prefer .length for all modern jQuery versions — including evergreen browsers with jQuery 3+.

0% Removed in 3.0
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.size() Gone

Bottom line: Replace every .size() with .length before upgrading to jQuery 3.

Conclusion

.size() answered one question: how many elements are in this jQuery object? That answer still matters — just read .length instead. Knowing .size() helps you understand legacy snippets and finish jQuery 3 migrations without surprises.

Next up: convert the matched set to a native array with .toArray().

💡 Best Practices

✅ Do

  • Use .length for every new count check
  • Replace .size() during jQuery 3 upgrades
  • Treat 0 as a normal empty-selection result
  • Count after .filter() / .find() when you need a subset
  • Cache the jQuery object if you count and then iterate

❌ Don’t

  • Call .size() on jQuery 3+ (it will throw)
  • Write new tutorials or apps that teach .size() as current API
  • Confuse matched-set count with .children().length unless you intend that
  • Assume .size() still exists because old blogs show it
  • Ship mixed codebases with both APIs after a partial migrate

Key Takeaways

Knowledge Unlocked

Five things to remember about .size()

The legacy count API — replaced by .length.

5
Core concepts
= 02

Same as

.length

Equivalent
! 03

Deprecated

1.8+

Legacy
× 04

Removed

3.0

Breaking
05

Use

.length

Modern

❓ Frequently Asked Questions

.size() returns an Integer: the number of elements currently in the jQuery collection. It takes no arguments. Functionally it is the same as reading the .length property. Available from jQuery 1.0 until it was removed in jQuery 3.0.
It was deprecated in 1.8 and removed in 3.0 because .length already provides the count without the overhead of a function call. Prefer .length in all modern code.
Use the .length property: var n = $("li").length; That works in every jQuery version, including 3.x and 4.x.
It counts elements in the current jQuery object — whatever your selector (or filter/find) returned — not automatically every descendant unless those descendants are in the matched set.
0. For example $("#missing").size() is 0 on jQuery 1.x/2.x, and $("#missing").length is 0 on all versions.
Yes in practice: .size is undefined, so .size() throws TypeError. Migrate to .length before upgrading to jQuery 3, or use a search-and-replace across your codebase.
Did you know?

The jQuery team documented that .size() existed mainly as a friendlier-looking method name for beginners, but the underlying storage was always the .length property shared with array-like collections. Removing the method in 3.0 simplified the API surface and nudged everyone toward the faster property access that JavaScript developers already use on arrays and NodeLists.

Next: jQuery .toArray() Method

Convert a jQuery collection into a native Array of DOM elements.

.toArray() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful