title: Cannot read property length of undefined array error in JavaScript?
body: <p>I am using the code below to get a list of colours used by 3 divs. If this div has a color set, then it should be pushed into the array <code>checkColors</code>.</p>
  
  <pre><code>var checkColors = [];
  
  $("div[style]").each(function(){
      getColor = $(this).css("color");
      if(getColor){
          checkColors.push(getColor);
      }
  });
  
  if(checkColors){
      checkColors = shuffleArray(checkColors);
      if(checkColors.length &gt; 0){
          if(checkColors[0]){
              backgroundColor = checkColors[0];
          }
          if(checkColors[1]){
              primaryColor = checkColors[1];
          }
          if(checkColors[2]){
              secondaryColor = checkColors[2];
          }
      }  
  }
  </code></pre>
  
  <p>However, <code>if(checkColors.length &gt; 0){</code> gives me a <code>Uncaught TypeError: Cannot read property 'length' of undefined</code> error which I don't understand since I have an If statement to check whether or not the array is undefined.</p>
  
  <p>Here is the <code>shuffleArray()</code> function:</p>
  
  <pre><code>    function shuffleArray(array) {
          for (var i = array.length - 1; i &gt; 0; i--) {
              var j = Math.floor(Math.random() * (i + 1));
              var temp = array[i];
              array[i] = array[j];
              array[j] = temp;
          }
      }
  </code></pre>