How to Check if an Array Is Empty or Null in JavaScript

How to check if a JavaScript array is empty, null or undefined, with Array.isArray(), length and optional chaining, plus the common mistakes to avoid.

Mahzaib MirzaMahzaib MirzaMay 1, 20232 min read0 comments
How to Check if an Array Is Empty or Null in JavaScript

To check whether a JavaScript value is a non-empty array, combine Array.isArray() with a length check. Array.isArray(arr) makes sure you have an array, which also covers null and undefined. arr.length tells you whether it has any items.

const arr = [];

if (!Array.isArray(arr) || arr.length === 0) {
  console.log('Not an array, or an empty array');
}

Why if (arr) is not enough

An empty array is truthy in JavaScript. A plain if (arr) check passes for [], so it tells you nothing about whether the array has items:

const empty = [];

if (empty) {
  console.log('This runs, even though the array is empty');
}

console.log(Boolean([])); // true

You need to check the length explicitly.

Check if an array is empty

If you already know the value is an array, compare its length to zero:

const items = [];

if (items.length === 0) {
  console.log('empty');
}

This throws a TypeError if items is null or undefined, so only use it on values you know are arrays.

Check if a value is null or undefined

Loose equality against null matches both null and undefined:

let list;

if (list == null) {
  console.log('null or undefined');
}

If your style guide bans loose equality, write list === null || list === undefined instead.

Check for null, undefined and empty in one go

Two reliable patterns cover all three cases. The first checks the type explicitly:

function isNullOrEmpty(arr) {
  return !Array.isArray(arr) || arr.length === 0;
}

isNullOrEmpty(null);      // true
isNullOrEmpty(undefined); // true
isNullOrEmpty([]);        // true
isNullOrEmpty([1, 2]);    // false

The second uses optional chaining. If arr is null or undefined, arr?.length evaluates to undefined instead of throwing:

if (!arr?.length) {
  console.log('null, undefined or empty');
}

The optional chaining version is shorter, but it does not check the type. A string like 'abc' has a length of 3 and passes as "not empty", and an object without a length property counts as "empty". Use it when the value is either an array or missing. Use Array.isArray() when it could be anything.

Check that an array has items

The opposite check reads more clearly in conditions:

function hasItems(arr) {
  return Array.isArray(arr) && arr.length > 0;
}

if (hasItems(users)) {
  users.forEach((user) => console.log(user.name));
}

Sparse arrays have a length but no items

new Array(3) creates an array with a length of 3 and three empty slots. MDN notes that an array's length can be greater than its number of elements when the array is sparse. A length check says it is not empty:

const sparse = new Array(3);

console.log(sparse.length);           // 3
console.log(sparse.some(() => true)); // false, no real items

some() skips empty slots, so arr.some(() => true) is true only if the array holds at least one real element. You rarely need this, but it explains the odd results some code produces.

Check if an array contains only null values

every() tests whether all items match a condition:

const values = [null, undefined, null];

const allEmpty = values.every((item) => item == null);
console.log(allEmpty); // true

Be careful with empty arrays here. [].every(...) always returns true, because there are no items that fail the test. Check the length first if that matters.

In TypeScript

A type guard lets the compiler know the array exists and is not empty after the check:

function hasItems<T>(arr: T[] | null | undefined): arr is T[] {
  return Array.isArray(arr) && arr.length > 0;
}

function printFirst(names: string[] | null | undefined) {
  if (hasItems(names)) {
    console.log(names[0]); // names is string[] here
  }
}

A common mistake in React

In JSX, {items.length && <List items={items} />} renders the number 0 on the page when the array is empty, because 0 is a valid thing to render. Compare explicitly instead:

{items.length > 0 && <List items={items} />}

Mistakes to avoid

  • Using if (arr) to test for items. Empty arrays are truthy.

  • Comparing with == false. [] == false is true because of type coercion, which makes the check confusing and hard to trust.

  • Reading .length on a possibly null value. Guard with Array.isArray() or optional chaining first.

  • Comparing an array to []. arr === [] is always false, because two different array objects are never equal.

Frequently asked questions

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

Check arr.length === 0. If the value might be null or undefined, check Array.isArray(arr) first or use arr?.length.

Is an empty array null in JavaScript?

No. An empty array is a real object with a length of 0. null means there is no value at all.

What does Array.isArray() return for null?

false. It returns true only for arrays, so it is safe to call on null and undefined.

Share:
Mahzaib Mirza

Written by

Mahzaib Mirza

Software developer & Founder of Coders Vibe.

Related Posts

Liked this post?

Get the next one in your inbox the moment it's published. No spam, unsubscribe anytime.

0 Comments

Leave a comment