jQuery size() Method

Beginner
5 min read
Updated: Oct 2026
5 examples
Legacy API

What Is size()?

.size() returned the number of elements in a jQuery collection (since 1.0). It was functionally the same as .length, but .length is preferred because it skips a function call. Official docs: deprecated in 1.8, removed in 3.0.

Remember
// Legacy (jQuery 1.x / 2.x only):
// $("li").size()

// Modern — use this everywhere:
$("li").length

// Same count, both used to alert "2" for two <li> items
alert("Size: " + $("li").length);
NeedUse
Count matches (modern)$(sel).length
Legacy count$(sel).size() — removed in 3.0
Native array of nodes.toArray()
One element by index.get(i)

How to Use (Modern: .length)

Select elements, read .length — do not call .size() on jQuery 3+.

  1. Build a jQuery collection with a selector or chain.
  2. Read .length to get the count as a number.
  3. Use that number in conditions, labels, or loops.
  4. When migrating, replace .size() with .length (drop the parentheses).
PieceRequired?Role
jQuery collectionYesThe matched set to count
ArgumentsNone.size() took none; .length is a property
Return—Integer count (0 if empty)

Pseudocode

Pseudocode
collection = matched elements

// Legacy:
// n = collection.size()     // function call

// Modern:
n = collection.length        // property read

return n  // integer ≥ 0

// Migration:
// .size()  →  .length

Cheat Sheet

Keep these facts handy when reading or migrating old code.

TopicDetail
Legacy syntax.size()
Modern syntax.length
Since1.0 · deprecated 1.8 · removed 3.0
ReturnInteger (matched count)
Empty set0
Why prefer lengthNo function-call overhead
jQuery 3+.size() → TypeError

Live Preview

Count demo items with modern .length (the replacement for .size()).

Browser demo of counting with .length — same idea as the examples below.

Result
Ready — pick a count.

jQuery Examples

Runnable demos use modern .length (works on jQuery 3+). Legacy .size() is noted where helpful.

Example 1 — Official Demo: Count List Items

Count matched <li> elements with .length.

jQuery
// <ul><li>foo</li><li>bar</li></ul>
// Legacy: $("li").size()
console.log($("li").length);  // 2
Try It Yourself

How It Works

  1. Match. Two list items are selected.
  2. Read length. The property returns 2.
  3. Same as size. Old .size() returned the same integer.
  4. Prefer length. No method call needed.

Example 2 — Official Pattern: Count Divs

Report how many div elements match after building the page.

jQuery
var n = $("div.box").length;
// Legacy: $("div.box").size()
console.log("There are " + n + " divs.");
Try It Yourself

How It Works

  1. Select. All div.box nodes match.
  2. Store count. n holds the integer length.
  3. Message. The string includes that number.
  4. Official idea. Same as the click-to-count demo, without the click.

Example 3 — Empty Selection Is 0

Missing selectors still give a safe count of zero.

jQuery
console.log($("#missing").length);  // 0
if ($(".modal").length === 0) {
  console.log("No modal on the page");
}
Try It Yourself

How It Works

  1. No match. The collection is empty.
  2. Length zero. Reading .length returns 0.
  3. Guard. Compare to 0 before acting.
  4. Safe. No error — just a count.

Example 4 — Migrate .size() → .length

One-line upgrade for legacy codebases.

jQuery
// Before (jQuery 1.x / 2.x):
// var n = $("p").size();

// After (all versions, including 3+):
var n = $("p").length;
console.log(n);
Try It Yourself

How It Works

  1. Find calls. Search the project for .size().
  2. Replace. Change each to .length (no parentheses).
  3. Same number. The count stays identical.
  4. jQuery 3 ready. The property still exists after the upgrade.

Example 5 — .size on jQuery 3

Confirm the method is gone before you ship a 3.x upgrade.

jQuery
console.log(typeof jQuery.fn.size);  // "undefined" on jQuery 3+
console.log($("li").length);         // still works
Try It Yourself

How It Works

  1. Check the fn. jQuery.fn.size is missing on 3.x.
  2. Calling would throw. $("li").size() → TypeError.
  3. Length stays. Counting still works via the property.
  4. Audit first. Migrate before bumping jQuery.

Edge Cases & Pitfalls

Watch these when reading legacy code or upgrading jQuery.

removed

Gone in jQuery 3

.size() throws. Replace with .length before upgrading.

parens

Property, not method

Write .length — not .length().

scope

Only the matched set

Descendants are not counted unless they are in the selection.

plugins

Old plugins may still call size

Audit third-party scripts when moving to jQuery 3+.

Browser Support

.size() existed in jQuery 1.0–2.x (deprecated since 1.8). It was removed in 3.0. Use .length in all modern browsers with jQuery 3+.

✓ Removed in jQuery 3.0 · Use .length

jQuery .size() → .length

.length works wherever jQuery runs in the browser. .size() only existed before jQuery 3.

N/A Legacy method
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
.length All jQuery

Bottom line: Do not call .size() on jQuery 3+. Count with $(sel).length instead.

Performance Notes

ApproachCostBest for
.lengthLowestAll modern counting
.size()Slightly higherLegacy only (removed in 3.0)

Official reason to prefer .length: it avoids a function call. For beginners the bigger win is compatibility with jQuery 3+.

Key Takeaways

  • Role: counted matched elements — same number as .length.
  • Status: deprecated 1.8, removed 3.0.
  • Replace: .size() → .length.
  • Empty: count is 0, not an error.

One line: do not use .size() — count with $(sel).length.

Frequently Asked Questions

.size() returned an integer: how many elements are in the jQuery collection. It took no arguments and matched .length. Added in jQuery 1.0, deprecated in 1.8, removed in 3.0.
Use the .length property: $('li').length. It works in every jQuery version, including 3.x and 4.x, and avoids a function-call overhead.
jQuery preferred .length because it is a property, not a method. Keeping both was redundant. Deprecated in 1.8; gone in 3.0.
No. .size is undefined, so calling .size() throws a TypeError. Replace every .size() with .length before upgrading.
0. For example $('#missing').length is 0 on all versions (and .size() was 0 on 1.x/2.x).
Only what is in the current matched set. Descendants are counted only if your selector or chain included them.

Next: toArray()

Convert a jQuery set 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.

9 people found this page helpful