October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk1 min

How to Check if an Array Is Empty in TypeScript

Check a known TypeScript array with `length === 0`; add a nullish guard or `Array.isArray` when the value may be missing or not an array.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a value that is already an array, check whether its length is zero: items.length === 0. If the value might be null, undefined, or not an array at all, guard or validate it before reading length.

Check a known array with length

JavaScript arrays expose a numeric length property. An empty array has a length of zero, so this is the direct check:

const isEmpty = items.length === 0;
const hasItems = items.length > 0;

Use strict equality (===) for the empty check. TypeScript documents array types and properties in its Object Types handbook.

Why items === [] does not work

Do not compare an array to a newly created empty array. Arrays are objects, and equality compares their references—not whether they contain the same elements. The newly written [] is a different array from items, so items === [] is false even when items has no elements. TypeScript 4.8 documents this comparison as always false in its release notes.

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.

Handle arrays that may be null or undefined

If the declared type allows either nullish value, check that the value exists before accessing length:

if (items != null && items.length === 0) {
  // items is present and empty
}

The items != null check excludes both null and undefined. TypeScript narrows the type after this guard, allowing the property access. The Narrowing handbook explains nullish checks and why broad truthiness checks can be inappropriate when a union includes other valid falsy values.

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
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Validate uncertain runtime input with Array.isArray

A TypeScript type does not by itself establish the shape of data received at runtime, such as an external input. Check the value first when it may not be an array:

if (Array.isArray(value)) {
  const isEmpty = value.length === 0;
}

Inside the condition, value is known to be an array, so it is safe to inspect length. TypeScript documents this use of Array.isArray for values with multiple possible types in TypeScript for JavaScript Programmers.

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

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

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.