jQuery size() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// 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);
CallWhat it does
size()Learn jQuery .size(): count matched elements (deprecated 1.8, removed 3.0), migrate to .length, empty sets, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Build a jQuery collection with a selector or chain.
Step 2Read .length to get the count as a number.
Step 3Use that number in conditions, labels, or loops.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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: .get() Method — Retrieve DOM Elements

Retrieve raw DOM elements from a jQuery set by index or as an array.

.get() Method — Retrieve DOM Elements tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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