title: Javascript: Why compare with null?
body: <p>An open source JavaScript project I work on includes code:</p>
  
  <pre><code>if (color) {
        tapeDiv.style.backgroundColor = color;
        // set color here if defined by event. Else use css
      }
  </code></pre>
  
  <p>A contributor wants to change it to </p>
  
  <pre><code>if (color != null) {  // this line changed
        tapeDiv.style.backgroundColor = color;
        // set color here if defined by event. Else use css
      }
  </code></pre>
  
  <p>color is a string var. Only a string of more than 0 characters should be used to explicitly set the color.</p>
  
  <p>Since JS casts "" and null as boolean false, why would the comparison != null be needed?</p>
  
  <p>Am I missing something in thinking that the first form is just as good (and a bit shorter) than the second?</p>
  
  <p>I see comparisons with null quite often in JS source. Why are they needed when all JS simple objects have known results when cast as booleans? </p>
  
  <p>Thanks,</p>
  
  <p>Larry</p>
  
  <p>ps. I suppose if 0 (an integer) was a valid case, then if(0) would be false [a problem] and if(0 != null) would be true [allows the 0 case]. Any other reason?</p>
  
  <p>pps. Should have mentioned that the tapeDiv is newly created. So there's no point to resetting the style to "" since the div is brand new.</p>