Human-readable bytes in JavaScript
Turns a byte count into something a person can read.
function formatBytes(bytes) {
if (bytes === 0) return "0 B";
const units = ["B", "KB", "MB", "GB", "TB"];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
const value = bytes / 1024 ** i;
return `${value.toFixed(value < 10 && i > 0 ? 1 : 0)} ${units[i]}`;
}
Keywords and builtins used here
Mathconstfunctionifreturn
The run, in numbers
- Lines
- 7
- Characters to type
- 264
- Tokens
- 90
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 64 seconds.
Step 4 of 5 in Text, step 9 of 43 in Language basics.