Sub topic 6
BigInt and Why It Refuses to Mix with Number
This chapter assumes you know the primitive types, how typeof works, and the Number edge cases from the previous chapter, especially the safe integer limit.
BigInt: Integers Without a Ceiling
A Number holds integers exactly only up to Number.MAX_SAFE_INTEGER (2^53 − 1). BigInt is a separate primitive type for integers of any size. You create one with an n suffix or with the BigInt() function.
const big = 9007199254740993n;
console.log(big + 1n); // 9007199254740994n
console.log(BigInt("123456789012345678901234567890")); // exact
console.log(7n / 2n); // 3n: division truncates toward zero
console.log(typeof big); // "bigint"Why mixing throws
The language could convert one operand to the other type, but either direction can lose information. A Number such as 1.5 has no BigInt equivalent. A large BigInt has no exact Number equivalent. JavaScript refuses to guess, so arithmetic that mixes the two throws a TypeError.
try {
1n + 1;
} catch (e) {
console.log(e.name); // "TypeError"
}
console.log(1n + BigInt(1)); // 2n: convert explicitly
console.log(Number(5n) + 1); // 6Common errors
| Expression | Result | Reason |
|---|---|---|
| `1n + 1` | TypeError | Arithmetic cannot mix the types |
| `+1n` | TypeError | Unary `+` would have to produce a Number |
| `BigInt(1.5)` | RangeError | The value is not an integer |
| `Math.max(1n, 2n)` | TypeError | `Math` functions accept only Numbers |
| `JSON.stringify({ a: 1n })` | TypeError | JSON has no BigInt format |
Comparisons are an exception. They compare mathematical values, so 1n < 2 is true and 1n == 1 is true. 1n === 1 is still false because the types differ.
How Primitives Get Methods: Wrapper Objects
Primitives are not objects, yet "hi".toUpperCase() works. When you access a property on a string, number, boolean, symbol, or bigint, the engine wraps the value in a temporary object (String, Number, and so on), looks up the property on that object's prototype, and then throws the wrapper away. This is called autoboxing.
Because the wrapper is temporary, properties you assign to a primitive do not persist. Sloppy-mode code ignores the assignment without an error. Strict-mode code throws a TypeError. null and undefined have no wrappers, so property access on them always throws.
Explicit wrappers are a trap
You can create wrappers with new, but they behave like objects, not like the primitives they hold.
const s = new String("a");
console.log(typeof s); // "object"
console.log(s === "a"); // false: object vs primitive
console.log(Boolean(new Boolean(false))); // true: every object is truthy
console.log(String(42)); // "42": without new, it converts
// new Symbol() and new BigInt() both throw TypeErrorCall String(), Number(), Boolean(), and BigInt() without new to convert values. Don't create wrappers with new.
Exercise
What does each line produce: (a) 10n 2n, (b) 10n 2, (c) 2n > 1, (d) typeof new Number(5)?
Show answer
(a) 20n. (b) TypeError, because arithmetic cannot mix BigInt and Number. (c) true, because relational comparisons accept mixed BigInt and Number operands. (d) "object", because new creates a wrapper object.
Exercise
Why does if (new Boolean(false)) { console.log("ran"); } print "ran"?
Show answer
new Boolean(false) is an object, and every object is truthy. The condition never looks at the false value stored inside.