Skip to content

Disallow useless array length check ​

💼 This rule is enabled in the ✅ recommended config.

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

Fail ​

js
if (array.length === 0 || array.every(Boolean));
js
if (array.length !== 0 && array.some(Boolean));
js
if (array.length > 0 && array.some(Boolean));
js
const isAllTrulyOrEmpty = array.length === 0 || array.every(Boolean);

Pass ​

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

Released under the Apache License 2.0.