In NO. 15.3.
// bad
if (collection.length) {
// ...
}
// good
if (collection.length > 0) {
// ...
}
why use collection.length>0 is better than collection.length?
Because .length isn't a boolean - which means you're relying on the implicit conversion of a number to a boolean. Using > 0 is explicit (and explicit > implicit), and avoids any possible confusion about what you mean.
I see,thanks for your wonderful reply. @ljharb
Most helpful comment
Because
.lengthisn't a boolean - which means you're relying on the implicit conversion of a number to a boolean. Using> 0is explicit (and explicit > implicit), and avoids any possible confusion about what you mean.