24

Is there any difference between checking an array's length as a truthy value vs checking that it's > 0?

In other words is there any reason to use one of these statements over the other:

var arr = [1,2,3];
if (arr.length) {
}

if (arr.length > 0) {
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Ben
  • 16,124
  • 22
  • 77
  • 122
  • if you know it's an array of objects (like a dom query result lib call), `if(arr[0])` works just fine too. – dandavis Oct 02 '15 at 16:16
  • 1
    Warm welcome to the people arguing over this in code review –  Jan 23 '21 at 14:13

3 Answers3

23

Is there any difference between checking an array's length as a truthy value vs checking that it's > 0?

Since the value of arr.length can only be 0 or larger and since 0 is the only number that evaluates to false, there is no difference.

In general, Boolean(n) and Boolean(n > 0) yield different results for n < 0.

In other words is there any reason to use one of these statements over the other

Only reasons related to code readability and understanding, not behavior.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 2
    +1 for that last sentence. You can do pretty much anything with JavaScript, even if it's stupid or hard to read. It takes a good developer to write readable code. – LoremIpsum Oct 02 '15 at 16:57
  • 2
    It's also debatable which of those is the more readable option. IMO it is `if (array.length)` but I know other people who prefer `if (array.length > 0) – rooby Jul 07 '20 at 01:17
15

array.length is fastest and shorter than array.length > 0. You can see difference of their speeds : http://jsperf.com/test-of-array-length

if(array.length){...} is similar to if(0){...} or if(false){...}

jbyrd
  • 5,287
  • 7
  • 52
  • 86
Sherali Turdiyev
  • 1,745
  • 16
  • 29
  • Interestingly enough `array.length` ran slower for me than `array.length > 0` in Chrome 59. Not sure how that's possible since intuitively it seems like it would be doing less. – Hanna Jul 11 '17 at 12:41
  • 1
    Chrome VM optimises in different ways each new version. If you have to think what the compiler is doing behind, knowing V8 is written in C++ and C++ is typed, the array length which is a number is treated as a boolean expression so I would say that in some cases it does the equivalent of `Boolean(array.length)` when the `> 0` is not present. There are a lots of different values that can be true or false in javascript and they all require a check (null, false, undefined, 0, '', etc.) – zeachco Mar 29 '21 at 15:52
11

In javascript, they dont make a difference.

In the end, whatever is in between the if parenthesis will evaluate to a truthy or falsey value

For all possible falsey values, see All falsey values in JavaScript

If you wish to be more clear and explicit, use

if (arr.length > 0) {
}

If you wish to be less verbose, use

if (arr.length) {
}
Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
  • 5
    To add emphasis to this answer, many developers choose clarity over being concise. It helps with code readability when scanning over it. Otherwise, there is no difference. – Mwr247 Oct 02 '15 at 16:16
  • More explicit yes. More clear though is debatable. In some cases, like this one, I think clarity is relatively equal with either option. Often I would go for the more verbose option but in this case I don't think it adds any value. I read it as "if the array has length", which is perfectly understandable IMO so I don't think the > 0 adds value. I think it actually just adds unnecessary noise, although maybe I've adapted to that over years of reading it without the > 0 so I can see how it would be easier that way for someone not used to it. (See, I'm usually pro verbosity :D) – rooby Jul 07 '20 at 01:24