Dealing with KB, MB, GB and TB and bytes
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];
}