October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober 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 if an Object Is Empty in TypeScript

For an ordinary non-null object, Object.keys(value).length === 0 checks for zero own enumerable string-keyed properties. Choose a different check for symbols, non-enumerable keys, Map, or Set.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For an ordinary, non-null object, check Object.keys(value).length === 0. This returns true when the object has no own, enumerable, string-keyed properties. It does not mean the object has no properties of every kind.

Check an ordinary object

Object.keys() returns the object’s own enumerable string-keyed property names. Comparing the resulting array’s length with zero is the concise default for an object literal:

As an Amazon Associate I earn from qualifying purchases.

function isEmptyObject(value: object): boolean {
  return Object.keys(value).length === 0;
}

This function assumes it receives a non-null object. The check is JavaScript runtime behavior; the TypeScript annotation describes the intended input but does not inspect its properties.

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

Decide what “empty” means

The Object.keys() check counts only own, enumerable, string-keyed properties. It ignores inherited properties, own non-enumerable properties, and symbol keys. For example, an object with only a non-enumerable property still produces an empty array from Object.keys().

Choose the API that matches the properties you want to count:

Check Properties counted
Object.keys(value).length === 0 Own, enumerable, string-keyed properties
Object.getOwnPropertyNames(value).length === 0 All own string-keyed properties, including non-enumerable ones
Reflect.ownKeys(value).length === 0 All own string and symbol keys, including non-enumerable ones
Object.entries(value).length === 0 Own, enumerable, string-keyed properties, like Object.keys()

Handle null and undefined deliberately

Object.keys() should not be called with null or undefined. If either may reach the helper, decide whether nullish input should count as empty or non-empty, then encode that policy:

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem
function isEmptyObject(value: object | null | undefined): boolean {
  return value != null && Object.keys(value).length === 0;
}

In this version, null and undefined return false. To treat them as empty instead, use value == null || Object.keys(value).length === 0. Choose one behavior based on what the caller means by “empty”; these are application policies, not interchangeable definitions.

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

Use collection-specific checks for Map and Set

A Map or Set can hold entries without exposing them as ordinary enumerable object properties. Check its collection contents with size:

const mapIsEmpty = map.size === 0;
const setIsEmpty = set.size === 0;

Similarly, an Object.keys() check is not automatically a useful emptiness test for arrays or class instances. Select a check that reflects the runtime value and the data you intend to count.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why the TypeScript type does not answer the question

TypeScript’s object type describes non-primitive values; it does not guarantee that a value is a plain object literal or define what “empty” means for it. At runtime, JavaScript examines the actual value. Use the relevant property-key check for ordinary objects and size for Map and Set.

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.

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

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
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.