Inconsistent behavior in JavaScript's conditional checking
<p>I want to build an object <code>obj1</code> with property <code>obj2</code>, which is another object.  To avoid redeclaring <code>obj1</code> and <code>obj2</code>, I use the following code:</p>

<pre><code>if (!obj1) obj1 = {};
if (!obj1.obj2) obj1.obj2 = {};
// code to use obj1
</code></pre>

<p>Assume, <code>obj1</code> and <code>obj1.obj2</code> aren't defined yet, the code causes the browser to report the error "obj1 is not defined".</p>

<p>If I change the code to:</p>

<pre><code>if (typeof obj1==='undefined') obj1 = {};
if (!obj1.obj2) obj1.obj2 = {};
// code to use obj1
</code></pre>

<p>Then there's no error, while I think it should report "obj2 is not defined".  I'm puzzled as to why the JavaScript treats the short-hand falsy check of a reference and a property differently.  Can anyone shed a light on that?</p>
