Sometimes, we want to convert file size to MB in JavaScript.
In this article, we’ll look at how to convert file size to MB in JavaScript.
How to convert file size to MB in JavaScript?
To convert file size to MB in JavaScript, we can divide the number of bytes by 1024 raise to the power of 2.
For instance, we write:
const bytesToMegaBytes = bytes => bytes / (1024 ** 2);
console.log(bytesToMegaBytes(4675758))
to define the bytesToMegaBytes
function that divide bytes
by 1024 ** 2
.
Therefore, we get 4.459150314331055 logged as a result.
Conclusion
To convert file size to MB in JavaScript, we can divide the number of bytes by 1024 raise to the power of 2.