What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
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:
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →#1 Best Overall
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 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.
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.
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.
Quick Recap
Best Value
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.




