Why do strings sometimes get changed to numbers in javascript?
<p>I have this code: </p>

<pre><code>function getSessionGUID() {
   return (S4()+S4());
}

function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
</code></pre>

<p>It clearly returns a string, but if you run it a bunch of times, you can notice that it sometimes returns infinity.</p>

<pre><code>for (var i = 0; i &lt; 100000; i++){ if(getSessionGUID() == Infinity) console.log("INFINITY"); }
871 x INFINITY
</code></pre>

<p>I then noticed that if you remove the |0, it solves the problem:</p>

<pre><code>function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
</code></pre>

<p>Results:</p>

<pre><code>for (var i = 0; i &lt; 100000; i++){ if(getSessionGUID() == Infinity) console.log("INFINITY"); }
undefined
</code></pre>

<p>Why does this happen? In both cases the value is changed into a string.</p>
