Explanation: The caret ^ is the XOR operator, XOR-ing a value with itself always returns zero, even in JS. But applying XOR to a BigInt (5n in this case) returns a zero of type BigInt.
So even though BigInt is a numeric-like value, it's not a Number.
The self-XOR is a shorthand for this:
typeof x?.valueOf() == 'bigint' ? 0n : 0
Except for the fact that Symbols will cause ^ to throw.
map replaces each element of an iterable (not in-place), every tests a predicate.
BigInt method that only works with Numbers
BigInt.asIntN(8, 257n) // 1n
BigInt.asIntN(8n, 257n) // throws an error
Explanation: The 1st parameter of that static method only accepts Number-typed arguments, more specifically, integer Numbers. That's because it specifies the exponent of the modulo divisor, which is expected to be small.
Conditionally-typed zero
Explanation: The caret
^
is the XOR operator, XOR-ing a value with itself always returns zero, even in JS. But applying XOR to aBigInt
(5n in this case) returns a zero of typeBigInt
.So even though BigInt is a numeric-like value, it's not a
Number
. The self-XOR is a shorthand for this:Except for the fact that
Symbol
s will cause^
to throw.map
replaces each element of an iterable (not in-place),every
tests a predicate.BigInt method that only works with Numbers
Explanation: The 1st parameter of that static method only accepts
Number
-typed arguments, more specifically, integerNumber
s. That's because it specifies the exponent of the modulo divisor, which is expected to be small.Links
More pitfalls/footguns/gotchas here: https://github.com/tc39/proposal-bigint#gotchas--exceptions Spec here: https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-bigint-objects Docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt