Empty-array checks show up everywhere: validating API payloads, guarding UI state, and branching before loops. In JavaScript the core idea is length === 0 on a real Array, but edge cases trip people up: values that are not arrays but expose length, optional values, and sparse arrays where length does not mean what it looks like.
This guide walks through the patterns to reach for first, when to combine Array.isArray with length, and pitfalls such as array-like objects, optional values, and sparse arrays.
Tested on: Node.js v20.18.2. After each snippet, a short note describes what you should see in the console.
Quick reference
Use this table for javascript check if array is empty and js array empty guards.
| Check | When to use |
|---|---|
arr.length === 0 |
You already have a real Array |
Array.isArray(x) && x.length === 0 |
Unknown / external input |
!(x?.length) |
“Missing or empty,” and non-arrays are ruled out or acceptable |
for…of + flag |
Rare; usually length is enough |
1. length === 0 (most direct)
When you already know the value is an array, comparing length to 0 is the clearest empty check.
const arr = [];
if (arr.length === 0) {
console.log("The array is empty.");
}You should see The array is empty.
2. Falsy length (!arr.length)
Because 0 is falsy, !arr.length is true when the array is empty. Many teams still prefer arr.length === 0 for readability.
const arr = [];
if (!arr.length) {
console.log("The array is empty.");
}Same message as section 1: The array is empty.
3. Array.isArray + length (when the value might not be an array)
For js check if array is empty on unknown input, check type first, then length—see MDN Array.isArray and array length.
const arr = [];
if (Array.isArray(arr) && arr.length === 0) {
console.log("The array is empty.");
}Again you should see The array is empty. when the value is [].
Why order matters: Array.isArray(x) is safe on null / undefined; reading x.length first can throw.
4. Pitfall — length without Array.isArray
An object can expose length: 0 without being an array:
const notArr = { length: 0 };
console.log(
notArr.length === 0,
Array.isArray(notArr),
Array.isArray(notArr) && notArr.length === 0,
);The log prints three values: true (length is zero), false (not an array), and false for the combined guard—so length alone is not enough.
So javascript array is empty should mean “value is an array and has no elements,” not merely “.length is zero.”
5. Optional chaining — “missing or empty”
When arr may be null or undefined, !(arr?.length) is true for missing or empty. It is not a substitute for Array.isArray if other types can appear.
const empty = [];
const filled = [1, 2];
console.log(!(null?.length));
console.log(!(undefined?.length));
console.log(!(empty?.length));
console.log(!(filled?.length));Four lines: true for null, true for undefined, true for the empty array, and false once elements exist.
6. reduce without an initial value (throws on [])
Calling reduce with no initial accumulator on an empty array throws TypeError. You can catch it, but length is simpler and faster.
const arr = [];
try {
arr.reduce((acc, current) => acc + current);
console.log("The array is not empty.");
} catch (error) {
if (error instanceof TypeError) {
console.log("The array is empty.");
}
}The catch path runs and prints The array is empty. because reduce without an initial value throws on [].
With an initial value, reduce does not throw on []—so this pattern is not a reliable emptiness test in general.
7. for…of loop (explicit scan)
Useful when you treat “empty” as “no iterable elements” the same way for…of does (same caveats as length for holes).
const arr = [];
let isEmpty = true;
for (const element of arr) {
isEmpty = false;
break;
}
console.log(isEmpty ? "The array is empty." : "The array is not empty.");The loop never runs, so you should see The array is empty.
8. Sparse arrays and new Array(n)
length counts slots, not “defined values.” A holey array can have length > 0 while still having no meaningful entries for some definitions of “empty.”
const holey = [,];
const sized = new Array(3);
console.log("holey length", holey.length, "is length===0?", holey.length === 0);
console.log("new Array(3) length", sized.length, "is length===0?", sized.length === 0);Both arrays report non-zero length, so each length === 0 check is false even though the slots are empty or holey.
If you need “no assigned elements,” combine length with Object.keys, filter, or similar—see also array copy / sparse notes.
Summary
- Prefer
arr.length === 0when you already hold anArray. - For unknown input, use
Array.isArray(x) && x.length === 0before trustinglength. - Remember
[]is truthy, objects can fakelength, and sparse arrays makelengthalone ambiguous.
References
MDN and community references for length, Array.isArray, and reduce behavior.
- MDN:
Array.prototype.length - MDN:
Array.isArray() - MDN:
Array.prototype.reduce() - Stack Overflow: check if array is empty or exists

