explicit-length-check
March 27, 2026 Β· View on GitHub
π Enforce explicitly comparing the length or size property of a value.
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
π§π‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
This rule is only meant to enforce a specific style and make comparisons more clear.
This rule is fixable, unless it's unsafe to fix.
Zero comparisons
Enforce comparison with === 0 when checking for zero length.
Examples
// β
const isEmpty = !foo.length;
// β
const isEmpty = foo.length == 0;
// β
const isEmpty = foo.length < 1;
// β
const isEmpty = 0 === foo.length;
// β
const isEmpty = 0 == foo.length;
// β
const isEmpty = 1 > foo.length;
// β
// Negative style is disallowed too
const isEmpty = !(foo.length > 0);
// β
const isEmpty = foo.length === 0;
// β
const isEmptySet = !foo.size;
// β
const isEmptySet = foo.size === 0;
<template>
<!-- β -->
<div v-if="!foo.length">Vue</div>
<!-- β
-->
<div v-if="foo.length === 0">Vue</div>
</template>
Non-zero comparisons
Enforce comparison with > 0 when checking for non-zero length.
Examples
// β
const isNotEmpty = foo.length !== 0;
// β
const isNotEmpty = foo.length != 0;
// β
const isNotEmpty = foo.length >= 1;
// β
const isNotEmpty = 0 !== foo.length;
// β
const isNotEmpty = 0 != foo.length;
// β
const isNotEmpty = 0 < foo.length;
// β
const isNotEmpty = 1 <= foo.length;
// β
const isNotEmpty = Boolean(foo.length);
// β
// Negative style is disallowed too
const isNotEmpty = !(foo.length === 0);
// β
const isNotEmpty = foo.length > 0;
// β
if (foo.length || bar.length) {}
// β
if (foo.length > 0 || bar.length > 0) {}
// β
const unicorn = foo.length ? 1 : 2;
// β
const unicorn = foo.length > 0 ? 1 : 2;
// β
while (foo.length) {}
// β
while (foo.length > 0) {}
// β
do {} while (foo.length);
// β
do {} while (foo.length > 0);
// β
for (; foo.length; ) {};
// β
for (; foo.length > 0; ) {};
Options
You can define your preferred way of checking non-zero length by providing a non-zero option (greater-than by default):
{
'unicorn/explicit-length-check': [
'error',
{
'non-zero': 'not-equal'
}
]
}
The non-zero option can be configured with one of the following:
greater-than(default)- Enforces non-zero to be checked with:
foo.length > 0
- Enforces non-zero to be checked with:
not-equal- Enforces non-zero to be checked with:
foo.length !== 0
- Enforces non-zero to be checked with:
This rule does not support Yoda-style comparisons like 0 < foo.length. If you use eslint/yoda, configure it to allow non-Yoda style for relational comparisons.
Unsafe to fix case
.length check inside some expressions are not safe to fix.
Example:
const bothNotEmpty = (a, b) => a.length && b.length;
if (bothNotEmpty(foo, bar)) {}
In this case, the bothNotEmpty function returns a number, but it will most likely be used as a boolean. The rule will still report this as an error, but without an auto-fix. You can apply a suggestion in your editor, which will fix it to:
const bothNotEmpty = (a, b) => a.length > 0 && b.length > 0;
if (bothNotEmpty(foo, bar)) {}
!foo.length used as the left side of a comparison is also unsafe to fix due to operator precedence.
// β
if (!foo.length > 0) {}
The rule is smart enough to know some LogicalExpressions are safe to fix, like when it's inside if, while, etc.