How to define an exabyte constant in JavaScript

1 Answer

0 votes
/*
    JavaScript Numbers are IEEE‑754 doubles.
    They can represent integers exactly only up to 2^53 - 1.

    Exabyte sizes:

        1 EiB = 2^60  = 1,152,921,504,606,846,976 bytes
        1 EB  = 10^18 = 1,000,000,000,000,000,000 bytes

    Both exceed 2^53, so we use BigInt for exact values.
*/

// Binary exabyte (exbibyte), using a BigInt bit shift.
// 1n << 60n = 2^60 bytes.
const EXABYTE_EIB = 1n << 60n;

// Decimal exabyte (SI), using a readable BigInt literal.
const EXABYTE_EB = 1_000_000_000_000_000_000n;

console.log("1 EiB =", EXABYTE_EIB, "bytes");
console.log("1 EB  =", EXABYTE_EB,  "bytes");



/*
run:

1 EiB = 1152921504606846976n bytes
1 EB  = 1000000000000000000n bytes

*/

 



answered May 3 by avibootz
...