title: Javascript: testing for at least one non-empty array (if one of the arrays may be null)
body: <p><strong>Given two arrays myArray1 and myArray2, which may be <code>null</code>, how can I output a Boolean which tells me if <em>at least one</em> array is non-empty?</strong></p>
  
  <p>Assuming that I have the following variables:</p>
  
  <pre><code>    myArray1 = ["one", "two", "three"]; //-&gt; non-empty array
      myArray2 = null; //-&gt; not an array (in my case this happens from .match() returning no results)
  </code></pre>
  
  <p>I want an expression, such that <code>myArray1 &amp;&amp; myArray2</code> will be FALSE, but <code>myArray1 || myArray2</code> will be TRUE.</p>
  
  <p>I did look through other relevant Stack Overflow questions (see an abridged list below), but since I still struggled to figure out the solution, I thought I would post it as a separate question since answers might also benefit others.</p>
  
  <p>The common way of testing for empty arrays is:</p>
  
  <pre><code>    myBooleanOr = (myArray1.length || myArray2.length); //3
      myBooleanAnd = (myArray1.length &amp;&amp; myArray2.length); //Error
  </code></pre>
  
  <p>This works if both variables are arrays, but in this case, the second one will throw up <strong><em>Error: cannot read property length of 'null'</em></strong>. Using the <code>Boolean()</code> function does not solve the problem since the following also throws up the same error: </p>
  
  <pre><code>    myBooleanAnd = (Boolean(myArray1.length) &amp;&amp; Boolean(myArray2.length)); //error
  </code></pre>
  
  <p>A solution for testing empty arrays which was accepted in several Stack Overflow questions is to use <code>typeof myArray !== "undefined"</code>, but that still does not solve the problem, because neither of the arrays match <code>"undefined"</code>, so myBooleanAnd will still throw up an error:</p>
  
  <pre><code>   var bool = (typeof myArray1 !== "undefined"); //true
     var bool = (typeof myArray2 !== "undefined"); //true
     var myBooleanAnd = ((typeof myArray1 !== "undefined" &amp;&amp; myArray1.length) || (typeof myArray2 !== "undefined" &amp;&amp; myArray2.length)); //Error: cannot read property length of null
  </code></pre>
  
  <p>Comparing the arrays against <code>[]</code>, which also seems intuitive, also doesn't work, because neither of the arrays match <code>[]</code>:</p>
  
  <pre><code>    var bool = (myArray1 !== []); //true
      var bool = (myArray2 !== []); //true
  </code></pre>
  
  <p><strong>Other relevant posts</strong></p>
  
  <p>A number of other questions on Stack Overflow deal with testing for empty Javascript arrays, including:</p>
  
  <ul>
  <li>Testing for empty arrays: <a href="https://stackoverflow.com/questions/11743392/check-if-array-is-empty-or-exists">Check if array is empty or exists</a></li>
  <li>Testing for empty arrays (jQuery): <a href="https://stackoverflow.com/questions/7198197/check-if-array-is-empty-or-null">Check if array is empty or null</a></li>
  <li>Relative advantages of methods for testing empty arrays: <a href="https://stackoverflow.com/questions/30506427/testing-for-an-empty-array?noredirect=1&amp;lq=1">Testing for an empty array</a></li>
  <li>Testing for empty objects: <a href="https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object">How do I test for an empty JavaScript object?</a></li>
  </ul>
  
  <p>And there are also questions about the truth value of empty arrays in Javascript:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/19146176/javascript-empty-array-evaluates-to-true-in-conditional-structures-why-is">JavaScript: empty array, [ ] evaluates to true in conditional structures. Why is this?</a></li>
  </ul>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have corrected the following errors posted in the original question (thanks to those who pointed them out). I am listing them here since they might also be helpful to others:</p>
  
  <ul>
  <li>==! changed to <code>!==</code></li>
  <li>typeof x === undefined, changed to <code>typeof x === "undefined"</code></li>
  </ul>