Skip to content

unicorn/explicit-length-check Pedantic

🛠️ An auto-fix is available for this rule for some violations.

What it does

Enforce explicitly comparing the length or size property of a value.

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 not-equal Enforces non-zero to be checked with: foo.length !== 0

Why is this bad?

Examples

Examples of incorrect code for this rule:

javascript
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;

const isEmpty = !foo.length;
const isEmpty = !(foo.length > 0);
const isEmptySet = !foo.size;

Examples of correct code for this rule:

javascript
const isEmpty = foo.length === 0;

References

Released under the MIT License.