Check if an array is empty in JavaScript

Check if array is empty in JavaScript with length, Array.isArray, optional chaining, and loop patterns—plus pitfalls (array-like objects, sparse arrays). Includes Node-tested console output.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

Check if an array is empty in JavaScript

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.

javascript
const arr = [];

if (arr.length === 0) {
  console.log("The array is empty.");
}
Output

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.

javascript
const arr = [];

if (!arr.length) {
  console.log("The array is empty.");
}
Output

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.

javascript
const arr = [];

if (Array.isArray(arr) && arr.length === 0) {
  console.log("The array is empty.");
}
Output

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:

javascript
const notArr = { length: 0 };

console.log(
  notArr.length === 0,
  Array.isArray(notArr),
  Array.isArray(notArr) && notArr.length === 0,
);
Output

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.

javascript
const empty = [];
const filled = [1, 2];

console.log(!(null?.length));
console.log(!(undefined?.length));
console.log(!(empty?.length));
console.log(!(filled?.length));
Output

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.

javascript
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.");
  }
}
Output

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).

javascript
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.");
Output

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.”

javascript
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);
Output

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 === 0 when you already hold an Array.
  • For unknown input, use Array.isArray(x) && x.length === 0 before trusting length.
  • Remember [] is truthy, objects can fake length, and sparse arrays make length alone ambiguous.

References

MDN and community references for length, Array.isArray, and reduce behavior.


Frequently Asked Questions

1. How do I check if an array is empty in JavaScript?

Use arr.length === 0 after you know the value is an array. If the value might not be an array, call Array.isArray(arr) first, then check length, to avoid trusting a length property on plain objects.

2. Is an empty array truthy or falsy in JavaScript?

An empty array [] is truthy. Do not write if (arr) to mean empty—you need an explicit length check (or Array.isArray plus length).

3. Can I use !arr.length to check if an array is empty?

Yes for real arrays: 0 is falsy so !arr.length is true when empty. Prefer arr.length === 0 when you want a strict, readable comparison.

4. What is wrong with checking only .length === 0?

Non-array objects can define a length property (for example { length: 0 }). length === 0 alone does not prove the value is an array—combine with Array.isArray when types vary.

5. Does a sparse array count as empty when length is 0?

If length is 0, there are no slots—[]. A sparse array can have length > 0 while holding only holes (for example new Array(3)); length does not count only defined values.

6. Does reduce on an empty array throw?

Array.prototype.reduce with no initial value throws TypeError on an empty array. That is not a good emptiness test—use length or Array.isArray plus length.
Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, …