JavaScript

JavaScript

By DeepSource

Bad collection size comparison JS-W1031

Anti-pattern
Autofix

Avoid using >= 0 and < 0 when comparing container sizes. When comparing collection sizes (array.length, set.size) with 0, it is recommended to use the > and == operators over the >= and < operators. Collections like arrays and sets always have a minimum size of 0. Therefore, >= 0 always evaluates to true and < 0 always evaluates to false.

Bad Practice

if (myArray.length >= 0) {
  // this block *always* executes
}

if (mySet.size < 0) {
  // this block *never* executes
}

Recommended

if (myArray.length > 0) {
  // block executes only if array is not empty
}

if (mySet.size == 0) {
  // block executes only if set is empty
}