jQuery .length

Beginner
5 min read
Updated: Oct 2026
5 examples
Live preview

What Is .length?

An Integer property: how many elements are currently matched in the jQuery object. Added in jQuery 1.0. It is a property — not a method — so write $("div").length, not $("div").length().

Remember
$("div").length  // Integer — count of matched elements

// Official idea: count, then grow the set
var n = $("div").length;
$("span").text("There are " + n + " divs.");

// Empty selection → 0 (no error)
// Prefer .length over removed .size()
NeedUse
Count matches$("li").length
Empty-set guardif ($(".item").length) { … }
After narrowing.filter() then .length
Visit each match.each()
Legacy count API.size() (removed — use length)
jQuery version string.jquery

How to Use .length

Select elements, then read the count on that collection.

  1. Build a jQuery set: $("div"), $(".item"), or a chain.
  2. Read .length (no parentheses).
  3. Use the Integer in a message, guard, or comparison.
  4. After .filter() / .add(), read .length again — it reflects the new set.
  5. Never call removed .size() in jQuery 3.x.
PieceRequired?Role
jQuery collectionYesResult of $() or a chain
.length—Integer count of matched elements
ArgumentsN/AProperty — not a function
Empty set—Returns 0, no throw

Pseudocode

Pseudocode
set = $("div")
n = set.length          // how many matched

if n === 0:
    nothing matched — skip work
else:
    update UI or loop with .each()

// After change:
set2 = set.filter(".active")
n2 = set2.length        // only the filtered count

// Official demo pattern:
on click:
    append a new div
    n = $("div").length
    show "There are " + n + " divs."

Cheat Sheet

Keep these facts handy while counting matches.

TopicDetail
TypeInteger property (not a method)
SincejQuery 1.0
Empty set0
vs .size()Same value; size removed in 3.0
After chainAlways current match count
Array-likeAlso index with [0], [1], …
Official demoClick → append div → update count text
Official docsapi.jquery.com/length/

Live Preview

Click to add a div and update the count — same idea as the official jQuery API demo.

Demo

Appends a <div> and reads $("#len-live-box div").length.

Count
Click “Add a div” to start.

jQuery Examples

Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.

Example 1 — Official: Count Divs on Click

Append a div, then show $("div").length.

jQuery
$(document.body)
  .on("click", function () {
    $(document.body).append($("
")); var n = $("div").length; $("span").text( "There are " + n + " divs. Click to add more." ); }) .trigger("click");
Try It Yourself

How It Works

  1. Click. Handler appends a new <div>.
  2. Count. $("div").length reads the match total.
  3. Show. Update the span text with n.
  4. Start. .trigger("click") runs once so the first count appears.

Example 2 — Empty Set Returns 0

Missing selectors do not throw — they count as zero.

jQuery
var n = $("#does-not-exist").length;
$("#out").text("Matches: " + n);
Try It Yourself

How It Works

  1. Select. Query an id that is not on the page.
  2. Read. .length is 0.
  3. Safe. No error — empty collections are normal in jQuery.
  4. Guard. Use if (n) before updating the DOM.

Example 3 — Count Inside a Container

Scope the selector so you only count children you care about.

jQuery
var n = $("#list li").length;
$("#out").text("List items: " + n);
Try It Yourself

How It Works

  1. Scope. #list li matches only items under #list.
  2. Count. .length returns how many li matched.
  3. Show. Print the integer in the output.
  4. Tip. Narrow selectors keep counts accurate on large pages.

Example 4 — .length After .filter()

Filtering creates a new set with its own count.

jQuery
var $all = $(".tag");
var $active = $all.filter(".active");
$("#out").text(
  "All: " + $all.length + "\\n" +
  "Active: " + $active.length
);
Try It Yourself

How It Works

  1. All. $(".tag").length counts every tag.
  2. Filter. .filter(".active") builds a smaller set.
  3. Active. That set’s .length is the subset count.
  4. Independent. Original $all.length is unchanged.

Example 5 — Guard Before .each()

Check .length first so empty sets skip the loop.

jQuery
var $items = $(".warn");
if ($items.length) {
  var lines = [];
  $items.each(function () {
    lines.push($(this).text());
  });
  $("#out").text("Found " + $items.length + ": " + lines.join(", "));
} else {
  $("#out").text("No warnings.");
}
Try It Yourself

How It Works

  1. Select. Build the set once and reuse it.
  2. Guard. if ($items.length) is falsy when empty.
  3. Loop. .each() runs only when there are matches.
  4. Else. Show a clear “none” message when count is 0.

Edge Cases & Pitfalls

Watch for these when using .length.

property

Not a method

Write $("div").length, not $("div").length().

removed

.size() is gone in jQuery 3

Calling .size() throws. Migrate every call to .length.

scope

Selector scope matters

$("div").length counts the whole document — scope to a container when you need a local total.

chain

Count the set you have

After .filter() or .find(), .length is for that new collection, not the previous one.

Browser Support

.length is a jQuery collection property available since 1.0. It works in every jQuery 1.x / 2.x / 3.x build. Prefer it over removed .size() in jQuery 3.0+.

✓ Stable · jQuery 1.0+

.length

Universal across jQuery 1.x–3.x. Read the Integer on any collection. Pair with .filter() and .each() for common match-count patterns.

100% With jQuery 1.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
.length jQuery 1.0+

Bottom line: Safe for production. Empty sets return 0. Replace any leftover .size() calls with .length.

Performance Notes

ApproachCostBest for
Read .lengthFreeAlready-built collections
.length vs .size()CheaperProperty read — no function call
Re-select every timeCan add upCache var $els = … then read $els.length

The property itself is free. Cost comes from re-running expensive selectors — select once, then read .length.

Key Takeaways

  • Role: Integer — how many elements are in this set.
  • Empty: returns 0 — no throw.
  • Official: append → read $("div").length → update text.
  • Migrate: use .length, never removed .size().

One line: .length answers “how many elements did this selection match?”

Frequently Asked Questions

.length is a Number on every jQuery collection. It returns how many DOM elements are currently in the set — for example $("li").length might return 5. Added in jQuery 1.0 and still supported in jQuery 3.x.
It returns 0. jQuery does not throw for empty selections — $("#missing").length is 0. Use if ($(".item").length) as a common guard before loops or updates.
Yes — they returned the same integer. .size() was deprecated in jQuery 1.8 and removed in jQuery 3.0. Always use .length in modern code. .length also avoids the overhead of a function call.
Yes. .length always reflects the current collection. $(".tag").filter(".active").length counts only the filtered subset.
Same idea — a numeric count — but jQuery .length counts DOM elements in the collection. jQuery objects are array-like: index [0] for the first element and read .length for the total.
Use .length for a quick check: if ($(".warn").length) { … }. Use .each() when you need to visit every matched element. Checking .length first avoids unnecessary work on empty sets.

Next: jQuery Selectors

Learn how selections are built — then count them with .length.

Selectors 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