How to define a terabyte constant in JavaScript

1 Answer

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

    Terabyte sizes:

        1 TiB = 2^40  = 1,099,511,627,776 bytes
        1 TB  = 10^12 = 1,000,000,000,000 bytes

    Both values are far below the safe integer limit,
    so using Number is perfectly fine.
*/

// Binary terabyte (tebibyte), using a bit shift.
// 1 << 40 = 2^40 bytes.
// const TERABYTE_TIB = 1 << 40;  // Error // bit‑shifts only work on 32‑bit integers

const TERABYTE_TIB = 2 ** 40        // or: 1n << 40n
// const TERABYTE_TIB = 1n << 40n;  // exact 2^40 as BigInt

// Decimal terabyte (SI), using a readable numeric literal.
const TERABYTE_TB = 1_000_000_000_000;

console.log("1 TiB =", TERABYTE_TIB, "bytes");
console.log("1 TB  =", TERABYTE_TB,  "bytes");



/*
run:

1 TiB = 1099511627776 bytes
1 TB  = 1000000000000 bytes

*/

 



answered May 3 by avibootz
...