title: How to check whether an object is empty?
body: <p>I'm learing JavaScript. I cannot grasp the idea of an empty object. As I understand, there are situations when I need to check a variable whether it holds an object and has a value.</p>
  <p>So far, I know that a variable can be <strong>undefined</strong>.</p>
  <pre><code>var car; // the value is undefined, as well as the type (which is it? number, array, etc.)
  </code></pre>
  <p>I also know, that everything that has a value, is <strong>true</strong>:</p>
  <pre><code>var car = &quot;Opel&quot;;
  Boolean(car); // true 
  </code></pre>
  <p>And anything without a value is false:</p>
  <pre><code>var car = &quot;&quot;; // typeof string, but it is empty, so
  Boolean(car); // false
  Boolean(null); // false 
  </code></pre>
  <p>So why doesn't it work the same way with arrays and objects? Why is all of this <strong>true</strong>? Shouldn't empty arrays and objects return <strong>false</strong>?</p>
  <pre><code>var car = {make: &quot;Opel&quot;, year: 2015};
  Boolean(car); // true
  car = {};
  Boolean(car); // true
  car = [&quot;BMW&quot;, &quot;Opel&quot;]; 
  Boolean(car); // true
  car = [];
  Boolean(car); // true
  </code></pre>
  <p>Now I see that there are methods, that can be applied to check an object's length, I just haven't reached that part yet.</p>
  <p><strong>I'm learning at W3Schools website and this bit just got me puzzled:</strong></p>
  <p>But you cannot test if an object is null, because this will throw an error if the object is undefined:</p>
  <p>Incorrect:</p>
  <pre><code>if (myObj === null) 
  </code></pre>
  <p>To solve this problem, you must test if an object is not null, and not undefined.</p>
  <p>But this can still throw an error:</p>
  <p>Incorrect:</p>
  <pre><code>if (myObj !== null &amp;&amp; typeof myObj !== &quot;undefined&quot;)
  </code></pre>
  <p>Because of this, you must test for not undefined before you can test for not null:</p>
  <p>Correct:</p>
  <pre><code>if (typeof myObj !== &quot;undefined&quot; &amp;&amp; myObj !== null)
  </code></pre>
  <p><strong>I still cannot understand the last line here.</strong></p>