title: Correct way to convert size in bytes to KB, MB, GB in JavaScript
body: <p>I got <a href="https://stackoverflow.com/questions/5501427/php-filesize-mb-kb-conversion">this code</a> to covert size in bytes via PHP.</p>
  
  <p>Now I want to convert those sizes to <em>human readable</em> sizes using JavaScript. I tried to convert this code to JavaScript, which looks like this:</p>
  
  <pre><code>function formatSizeUnits(bytes){
    if      (bytes &gt;= 1073741824) { bytes = (bytes / 1073741824).toFixed(2) + " GB"; }
    else if (bytes &gt;= 1048576)    { bytes = (bytes / 1048576).toFixed(2) + " MB"; }
    else if (bytes &gt;= 1024)       { bytes = (bytes / 1024).toFixed(2) + " KB"; }
    else if (bytes &gt; 1)           { bytes = bytes + " bytes"; }
    else if (bytes == 1)          { bytes = bytes + " byte"; }
    else                          { bytes = "0 bytes"; }
    return bytes;
  }
  </code></pre>
  
  <p>Is this the correct way of doing this? Is there an easier way?</p>