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()returnsfalsefor an empty array. There is no need to check if the array is not empty.Array#every()returnstruefor 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);