October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Check Whether an Array Is Empty in jQuery

Use array.length === 0 for a JavaScript array. For a jQuery selector result, check the collection’s .length; don’t use $.isEmptyObject() as an array test.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a JavaScript array, check array.length === 0. jQuery does not change how JavaScript arrays work. If you mean a jQuery selector result, check the collection’s .length instead.

Check whether a JavaScript array is empty

An array is empty when its length is zero:

if (array.length === 0) {
  // The array has no slots
}

To check for the opposite condition, use array.length > 0. The array’s length counts its slots, not just slots that have assigned values. MDN explains how array length works.

Check the type first if the value is uncertain

If a variable might not contain an array, use the native Array.isArray() check before reading its length:

if (Array.isArray(value) && value.length === 0) {
  // value is an empty array
}

Array.isArray() returns false for values such as null, undefined, ordinary objects, and typed arrays. It also recognizes arrays created in another JavaScript realm, where instanceof Array can fail. See MDN’s Array.isArray() reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Check a jQuery selector result separately

A jQuery selection returns a jQuery collection, not a plain JavaScript array. To determine whether a selector matched anything, check the collection’s .length:

if ($(selector).length === 0) {
  // No elements matched
}

For example, if ($(".notice").length) is also a valid truthy check for whether the selection found at least one element. An unmatched selector produces an empty jQuery object with .length equal to 0. The jQuery constructor documentation describes selector results.

Rank #2
Sale
JavaScript and jQuery: Interactive Front-End Web Development
  • JavaScript Jquery
  • Introduces core programming concepts in JavaScript and jQuery
  • Uses clear descriptions, inspiring examples, and easy-to-follow diagrams

Why $.isEmptyObject() is not the array check

$.isEmptyObject() checks whether an object has enumerable properties; it is not the semantic test for whether an array has zero slots. The jQuery API says: “Check to see if an object is empty (contains no enumerable properties).” It also says the argument should generally be a plain JavaScript object. For array emptiness, use the array’s length. jQuery.isEmptyObject() API documentation.

Empty slots and sparse arrays

length === 0 answers whether an array has zero slots. A sparse array can contain unassigned slots and still have a positive length; therefore, this check does not tell you how many slots contain assigned values. If your application needs to distinguish those cases, define and check that condition separately.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use current native APIs

The jQuery API index marks jQuery.isArray() as deprecated in jQuery 3.2 and removed, and jQuery.isEmptyObject() as deprecated in jQuery 3.3 and removed. For array type validation, use Array.isArray(); for emptiness, inspect .length. jQuery 3.2 API changes and jQuery 3.3 API changes.

Quick Recap

SaleBestseller No. 1
Web Design with HTML, CSS, JavaScript and jQuery Set
Web Design with HTML, CSS, JavaScript and jQuery Set
Brand: Wiley; Set of 2 Volumes
$35.05
SaleBestseller No. 2
JavaScript and jQuery: Interactive Front-End Web Development
JavaScript and jQuery: Interactive Front-End Web Development
JavaScript Jquery; Introduces core programming concepts in JavaScript and jQuery; Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
$22.75

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.