Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

How to Check if an Array Is Not Empty in TypeScript

For a known TypeScript array, use items.length > 0 to check that it contains at least one element. Handle nullish and unknown values before reading length.

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 value you already know is an array, check items.length > 0. It is true when the array contains at least one element. If the reference might be missing or the value comes from outside your program, first handle that uncertainty before reading .length.

Check the length of a known array

Use the array’s length property:

if (items.length > 0) {
  // items contains at least one element
}

The equivalent non-empty test is items.length !== 0. To take a branch when it has no elements, use items.length === 0. An empty array has a length of zero.

Handle an optional or nullable array

If items can be null or undefined, check for a value before accessing its length:

if (items && items.length > 0) {
  // items is present and non-empty
}

With strictNullChecks, TypeScript requires you to account for null and undefined before using properties on a possibly nullish value. If both a missing array and an empty array should mean “no items,” use optional chaining and nullish coalescing:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const hasItems = (items?.length ?? 0) > 0;

This expression evaluates to false when items is missing, and also when it is present but empty. If your program needs to treat those cases differently, test for null or undefined separately instead of collapsing them into one result.

Validate values from an unknown source

A value typed as unknown, or received from an untrusted runtime boundary such as parsed data, is not guaranteed to be an array. Check its runtime type first:

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
if (Array.isArray(value) && value.length > 0) {
  // value is an array with at least one element
}

Array.isArray establishes that the value is an array before the length check. TypeScript’s narrowing handbook documents it as a runtime array check.

Why array truthiness and equality do not work

if (items) checks presence, not occupancy

Arrays are objects, and even an empty array is truthy in JavaScript. So if (items) does not tell you whether it contains elements; use items.length > 0 for that.

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

items === [] compares references

Do not test emptiness with items === [] or items == []. The array literal creates a new array, and JavaScript compares arrays by object identity rather than by their contents. Use items.length === 0 for an empty-array check. The TypeScript 4.8 release notes call out this array-literal equality pitfall.

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

Array length is different from element truthiness

items.length > 0 means at least one array element exists; it does not mean any element is truthy or usable. Arrays such as [0], [false], and [""] are non-empty.

If the requirement is “at least one value that is not undefined,” express that condition directly rather than using a general truthiness test. For example, a truthiness filter would also discard the valid numeric value 0. TypeScript’s 5.5 release notes discuss this distinction in the context of filtering values.

Choose the check that matches the input

Situation Check Meaning
Known array; non-empty branch items.length > 0 At least one element exists.
Known array; empty branch items.length === 0 No elements exist.
Nullable array; missing and empty both mean no items (items?.length ?? 0) > 0 True only when the array is present and has positive length.
Unknown runtime value Array.isArray(value) && value.length > 0 Checks that the value is an array, then checks its length.

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 *

Free tools Windows power users keep installed

One-click scans. No signup required.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
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.