2

Are there any advantages to using one of following statements over the other to check for an empty array?

if (arr.length) {}

vs

if (arr.length > 0) {}

I'm assuming they are the same, but I haven't seen any examples online of the first example.

user3783301
  • 143
  • 1
  • 1
  • 4

4 Answers4

3
if(typeof arr != "undefined" && arr != null && arr.length > 0){}

I was always told this was the correct way to check for empty arrays, and it's stuck with me.

So, to answer your question precisely:

if(arr.length > 0){}

Is the method I was led to believe was "correct", as it is clearer to anyone reading the code what the implied logic was. I believe this is likely why you see this method used in documentation/samples/tutorials over the other.

But as for any specific advantage other than semantic look and feel, I don't believe there is any.

A lot more explanation available on this question: Check if array is empty or exists

(linking, as I don't want to steal their descriptions - they deserve the upvotes)

Community
  • 1
  • 1
Sk93
  • 3,676
  • 3
  • 37
  • 67
  • 2
    This is off-topic. OP doesn't want to know if his variable is defined. Doing this at every test of array length makes no sense. – Denys Séguret May 28 '15 at 12:28
  • @DenysSéguret - expanded my answer to cover the specifics of the question, whilst leaving the more "cover all ground" check in the answer to help provide a broader check and avoid confusion between an array with no entries, and a null / undefined array, which may benefit other readers of this question, if not the original questioner. – Sk93 May 28 '15 at 13:30
1

Since arr.length can't be negative, they will result in the same behaviour. In the first one, the integer value arr.length will be evaluated as true or false in a Boolean context (it's "truthy"): false if 0 and true otherwise. So it is the same as if(arr.length != 0). It all depends on what you think is the most legible.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
David van rijn
  • 2,108
  • 9
  • 21
0

Generally, it should be the same because 0 is recognized as "false" (when converted into a boolean). But having a ">0" will make the code more readable, so I recommend using

if (arr.length > 0) {}

Speed-wise, they should be identical, or nearly so.

Yuri Predborski
  • 186
  • 1
  • 1
  • 12
0

One main difference would be for ease of code when testing multiple arrays for instance

if (arr.length && arr2.length && arr3.length) {}

This would be a lot easier than specifying > 0 constantly. Its another one of those things that is does the browser or javascript version support the shorthand. If your target audience was for people with an outdated browser you would probably want to use the full version.

Luke Rixson
  • 607
  • 5
  • 20
  • It's not dependent on JavaScript or browser version; this is a "feature" that has been in JavaScript for at least 18 years ([ECMAScript 1.0, 1997](http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%201st%20edition,%20June%201997.pdf)). – Frxstrem May 28 '15 at 12:38