no-useless-length-check

March 27, 2026 ยท View on GitHub

๐Ÿ“ Disallow useless array length check.

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

  • Array#some() returns false for an empty array. There is no need to check if the array is not empty.
  • Array#every() returns true for an empty array. There is no need to check if the array is empty.

We only check .length === 0, .length !== 0, and .length > 0. These zero and non-zero length check styles are allowed in the unicorn/explicit-length-check rule. It is recommended to use them together.

Examples

// โŒ
if (array.length === 0 || array.every(Boolean));
// โŒ
if (array.length !== 0 && array.some(Boolean));

// โœ…
if (array.every(Boolean));
// โŒ
if (array.length > 0 && array.some(Boolean));

// โœ…
if (array.some(Boolean));
// โŒ
const isAllTrulyOrEmpty = array.length === 0 || array.every(Boolean);

// โœ…
const isAllTrulyOrEmpty = array.every(Boolean);
// โœ…
if (array.length === 0 || anotherCheck() || array.every(Boolean));
// โœ…
const isNonEmptyAllTrulyArray = array.length > 0 && array.every(Boolean);
// โœ…
const isEmptyArrayOrAllTruly = array.length === 0 || array.some(Boolean);