title: Javascript replace null
body: <pre><code>var p = "null"
  var q = null;
  (p == q) //false. as Expected.
  
  p.replace(null, "replaced") // outputs replaced. Not expected.
  p.replace("null", "replaced") //outputs replaced. Expected.
  
  q.replace(null, "replaced") // error.  Expected.
  q.replace("null", "replaced") //error. Expected.
  </code></pre>
  
  <p>Why? Does replace not differentiate between <code>"null"</code> and <code>null</code>? </p>
  
  <p>I ask because I ran into a bug in angularjs:</p>
  
  <pre><code>replace((pctEncodeSpaces ? null : /%20/g), '+');
  </code></pre>
  
  <p>If for example, someone had a username of <code>"null"</code> and it was used as url, it would be replaced with "+" on any <code>$http</code> calls. e.g. <code>GET /user/null</code>. </p>
  
  <p>Not that this scenario would occur often, but I'm more curious why replace treats <code>null</code> and <code>"null"</code> as the same thing. Does replace do a .tostring on <code>null</code> before it does the replacement? Is this just a quirk of Javascript?</p>
  
  <p>I verified this on both IE and Chrome's implementations of <code>replace</code>.</p>