title: Number.sign() in javascript
body: <p>Wonder if there are any nontrivial ways of finding number's sign (<a href="http://en.wikipedia.org/wiki/Signum_function" rel="noreferrer">signum function</a>)?<br>
  May be shorter / faster / more elegant solutions than the obvious one</p>
  
  <pre><code>var sign = number &gt; 0 ? 1 : number &lt; 0 ? -1 : 0;
  </code></pre>
  
  <hr>
  
  <h2>Short answer!</h2>
  
  <p>Use this and you'll be safe and fast (source: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign" rel="noreferrer">moz</a>)</p>
  
  <pre class="lang-js prettyprint-override"><code>if (!Math.sign) Math.sign = function(x) { return ((x &gt; 0) - (x &lt; 0)) || +x; };
  </code></pre>
  
  <p>You may want to look at performance and type-coercing comparison <a href="http://jsfiddle.net/disfated/NJFSa/" rel="noreferrer">fiddle</a></p>
  
  <blockquote>
    <p>Long time has passed. Further is mainly for historical reasons.</p>
  </blockquote>
  
  <hr>
  
  <h2>Results</h2>
  
  <p>For now we have these solutions:</p>
  
  <hr>
  
  <p><strong>1.</strong> Obvious and fast</p>
  
  <pre><code>function sign(x) { return x &gt; 0 ? 1 : x &lt; 0 ? -1 : 0; }
  </code></pre>
  
  <hr>
  
  <p><strong>1.1.</strong> Modification from <a href="/users/854291/kbec">kbec</a> - one type cast less, more performant, shorter <strong>[fastest]</strong></p>
  
  <pre><code>function sign(x) { return x ? x &lt; 0 ? -1 : 1 : 0; }
  </code></pre>
  
  <p><em>caution:</em> <code>sign("0") -&gt; 1</code></p>
  
  <hr>
  
  <p><strong>2.</strong> Elegant, short, not so fast <strong>[slowest]</strong></p>
  
  <pre><code>function sign(x) { return x &amp;&amp; x / Math.abs(x); }
  </code></pre>
  
  <p><em>caution:</em> <code>sign(+-Infinity) -&gt; NaN</code>, <code>sign("0") -&gt; NaN</code></p>
  
  <p>As of <code>Infinity</code> is a legal number in JS this solution doesn't seem fully correct.</p>
  
  <hr>
  
  <p><strong>3.</strong> The art... but very slow <strong>[slowest]</strong></p>
  
  <pre><code>function sign(x) { return (x &gt; 0) - (x &lt; 0); }
  </code></pre>
  
  <hr>
  
  <p><strong>4.</strong> Using bit-shift<br>
      <em>fast, but <code>sign(-Infinity) -&gt; 0</code></em></p>
  
  <pre><code>function sign(x) { return (x &gt;&gt; 31) + (x &gt; 0 ? 1 : 0); }
  </code></pre>
  
  <hr>
  
  <p><strong>5.</strong> Type-safe <strong>[megafast]</strong></p>
  
  <p><strong>!</strong> Seems like browsers (especially chrome's v8) make some magic optimizations and this solution turns out to be much more performant than others, even than (1.1) despite it contains 2 extra operations and logically never can't be faster.</p>
  
  <pre><code>function sign(x) {
      return typeof x === 'number' ? x ? x &lt; 0 ? -1 : 1 : x === x ? 0 : NaN : NaN;
  }
  </code></pre>
  
  <hr>
  
  <h2>Tools</h2>
  
  <ul>
  <li><a href="http://jsperf.com/signs/3" rel="noreferrer">jsperf</a> preformance tests;</li>
  <li><a href="http://jsfiddle.net/disfated/NJFSa/" rel="noreferrer">fiddle</a> - type-cast tests;</li>
  </ul>
  
  <p>Improvements are welcome!</p>
  
  <hr>
  
  <h3>[Offtopic] Accepted answer</h3>
  
  <ul>
  <li><p><a href="/a/14418335/489553">Andrey Tarantsov</a> - +100 for the art, but sadly it is about 5 times slower than the obvious approach</p></li>
  <li><p><a href="/a/7624945/489553">Frédéric Hamidi</a> - somehow the most upvoted answer (for the time writing) and it's kinda cool, but it's definitely not how things should be done, imho. Also it doesn't correctly handle Infinity numbers, which are also numbers, you know.</p></li>
  <li><p><a href="/a/9079549/489553">kbec</a> - is an improvement of the obvious solution. Not that revolutionary, but taking all together I consider this approach the best. Vote for him :)</p></li>
  </ul>