title: JavaScript undefined replaced with null
body: <p>In JavaScript <code>undefined</code> can be reassigned, so it is often advised to create a self executing function that assures undefined is actually undefined.  As an alternative <code>null</code> and <code>undefined</code> are definitely <code>==</code> but are any other values loosely equivalent to <code>null/undefined</code>?</p>
  
  <h1>TLDR</h1>
  
  <p>Basically can you safely replace this:</p>
  
  <pre><code>(function(undefined){
  
     window.f = function(obj){
       if(obj===undefined || obj===null ){
         alert('value is undefined or null');
       }
     }
  
  })();
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>window.f = function(obj){
    if(obj==null){
      alert('value is undefined or null');
    }
  }
  </code></pre>
  
  <p>If the above is 100% safe, why doesn't the JavaScript community/libraries drop <code>undefined</code> altogether and use the shorter <code>x == null</code> conditional to check for both <code>null</code>/<code>undefined</code> at once?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I have never seen someone actually represent an "unknown value" with 'undefined' vs <code>null</code>? I have never seen this scenario, and is why I originally asked the question. It just appears to be two incredibly confused values that are never used in their original intent. Standardizing everything to do a comparison <code>obj==null</code> would be beneficial for size and avoid any issues with reassignment. Everything would continue to work </p>
  
  <pre><code>var obj={};
  obj.nonExistantProperty==null // true
  
  var x;
  ix==null // true
  
  function(obj){
    obj==null // true
  }
  </code></pre>
  
  <p>The one exception to this rule appears to be when casting <code>undefined</code>/<code>null</code> to an integer.  This is a pretty age case scenario, but definitely should be noted.</p>
  
  <p><code>+(null)==0</code>
  while
  <code>isNaN(+undefined)</code></p>
  
  <p>Considering NaN is the only value in JavaScript not equal to itself, you can do some pretty crazy things like:</p>
  
  <pre><code>+undefined == +undefined // false
  +null == +null // true
  </code></pre>
  
  <p>Using <code>null</code> as a loose equality <code>==</code> drop in replacement for <code>undefined</code> is safe, provided you don't plan to cast the value to an integer.  Which is a pretty edge case scenario.</p>