2

Is this the case in all browser versions? Meaning, an empty array is always considered as TRUE and never as FALSE as a boolean representation?

var x = [];

if(x)
   alert('this could be an empty array');
else
    alert('this could NEVER be an empty array');
Charles
  • 50,943
  • 13
  • 104
  • 142
Simon Ferndriger
  • 4,455
  • 6
  • 28
  • 53

5 Answers5

3

According to ECMA Script 5.1 specification's Boolean expression evaluation function, any object will be always evaluated to be Truthy. So, an array will be always evaluated to be truthy.

+-----------------------------------------------------------------------+
| Argument Type | Result                                                |
|:--------------|------------------------------------------------------:|
| Undefined     | false                                                 |
|---------------|-------------------------------------------------------|
| Null          | false                                                 |
|---------------|-------------------------------------------------------|
| Boolean       | The result equals the input argument (no conversion). |
|---------------|-------------------------------------------------------|
| Number        | The result is false if the argument is +0, −0, or NaN;|
|               | otherwise the result is true.                         |
|---------------|-------------------------------------------------------|
| String        | The result is false if the argument is the empty      |
|               | String (its length is zero); otherwise the result is  |
|               | true.                                                 |
|---------------|-------------------------------------------------------|
| Object        | true                                                  |
+-----------------------------------------------------------------------+

As per the last line, for any Object, result will be true.

Reference: My answer to the other question in SO

Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
1

Arrays are truthy, yes. If you want an easy way to check for emptiness, use the length property:

var x = [];

if(x.length)
    alert('this array is not empty');
Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
0

Yes. All objects are truthy. Always.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Yes. Array is an object. So it is true. whereas, null/undefined is false

vivek_nk
  • 1,590
  • 16
  • 27
0

by default these are things are treated as false in javascript.

  1. false
  2. null
  3. undefined
  4. '' or "" [empty string]
  5. 0 [zero]
Pushker Yadav
  • 866
  • 1
  • 8
  • 15
  • "By default"? If you're going to say things like that, you should also at least give a cursory mention of when that behavior does not apply. (Though I can't think of any circumstances where the above is not true.) – Platinum Azure Apr 09 '14 at 12:29
  • simple, you using other than these then result will be true. – Pushker Yadav Apr 09 '14 at 12:34