jQuery .length

Beginner
5 min read
Updated: Nov 2026
3 examples
Properties

Definition

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

Syntax

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

jQuery
$("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()
CallWhat it does
.lengthLearn jQuery .length: count DOM elements in a collection, empty sets return 0, official click-to-add demo, updates after filter, prefer over removed .size(), examples, 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 set: $("div"), $(".item"), or a chain.
Step 2Read .length (no parentheses).
Step 3Use the Integer in a message, guard, or comparison.

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

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.

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.

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: Tutorial for Beginners: What Is jQuery?

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

Tutorial for Beginners: What Is jQuery? 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