Dealing with KB, MB, GB and TB and bytes This function is simple, it gets a number of bytes and returns its representation in either Bytes, KB, MB, GB and TB.

As simple as it is, I am sure there are other (and perhaps better ways) to write it.

function kmgtbytes (num) {
    if (num &gt; 0 ){
        if (num &lt; 1024)             { return [num, "Bytes"] }
        if (num &lt; 1048576)          { return [num/1024, "KB"] }
        if (num &lt; 1073741824)       { return [num/1024/1024, "MB"] }
        if (num &lt; 1099511600000)    { return [num/1024/1024/1024, "GB"] }

        return [num/1024/1024/1024/1024, "TB"]
    }

    return num
}
Here's a slightly less repetitive way to write it since we know each unit is 1024 as large as the last:

function kmgtbytes (num) {
    var unit, units = ["TB", "GB", "MB", "KB", "Bytes"];
    for (unit = units.pop(); units.length &amp;&amp; num &gt;= 1024; unit = units.pop()) {
        num /= 1024;
    }
    return [num, unit];
}