Bytes to Human Readable

Convert bytes to human readable format (KB, MB, GB, etc.) for file sizes.

Code

Utilities
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
let i = 0;
let size = bytes;
while (size >= 1024 && i < units.length - 1) { size /= 1024; i++; }
return `${size.toFixed(2)} ${units[i]}`;

Parameters

Number of bytes.

Browser·fetch() may be limited by CORS

More JavaScript Snippets