BigInt in JavaScript
Miscellaneous: BigInt
What is BigInt in JavaScript?
View Answer:
const bigint = 1234567890123456789012345678901234567890n;
const sameBigint = BigInt('1234567890123456789012345678901234567890');
const bigintFromNumber = BigInt(10); // same as 10n
console.log(1n + 2n); // 3
console.log(5n / 2n); // 2
console.log(1n + 2); // Error: Cannot mix BigInt and other types
let bigint = 1n;
let number = 2;
// number to bigint
console.log(bigint + BigInt(number)); // 3
// bigint to number
console.log(Number(bigint) + number); // 3
How do you declare a BigInt in JavaScript?
View Answer:
const bigint = 1234567890123456789012345678901234567890n;
const sameBigint = BigInt('1234567890123456789012345678901234567890');
const bigintFromNumber = BigInt(10); // same as 10n
Why was BigInt introduced in JavaScript?
View Answer:
Can you perform mathematical operations with BigInts and Numbers together?
View Answer:
const myBigInt = BigInt("12345678901234567890");
const myNumber = 10;
const result = Number(myBigInt) + myNumber;
console.log(result); // 12345678901234567000
How does the division operator interact with BigInts?
View Answer:
// Regular Numbers
console.log(5 / 2); // 2.5
// BigInt
console.log(5n / 2n); // 2, rounds towards zero
How does BigInt handle decimal numbers?
View Answer:
const myBigInt = BigInt(3.14159);
console.log(myBigInt); // RangeError: The number 3.14159 cannot be converted to a BigInt because it is not an integer
const anotherBigInt = BigInt("2.71828");
console.log(anotherBigInt); // SyntaxError: Cannot convert 2.71828 to a BigInt
Can BigInt be used as a key in a Map or Set?
View Answer:
Here's an example of using BigInt as a key in a Map
:
const myMap = new Map();
const keyBigInt = BigInt("12345678901234567890");
const value = "Some value";
myMap.set(keyBigInt, value);
console.log(myMap.get(keyBigInt)); // Output: "Some value"
You can use BigInt as a value in a Set
:
const mySet = new Set();
const valueBigInt = BigInt("12345678901234567890");
mySet.add(valueBigInt);
console.log(mySet.has(valueBigInt)); // Output: true
What happens when you mix regular numbers with BigInts?
View Answer:
console.log(1n + 2); // Error: Cannot mix BigInt and other types
// Explicit Conversion
let bigint = 1n;
let number = 2;
// number to bigint
console.log(bigint + BigInt(number)); // 3
// bigint to number
console.log(Number(bigint) + number); // 3
Is it possible to use the unary operator on BigInts?
View Answer:
let bigIntVal = BigInt(10);
console.log(-bigIntVal); // Outputs: -10n
In this example, the unary '-' operator is used to negate a BigInt.
However, using the '+' operator will result in a TypeError:
let bigIntVal = BigInt(10);
console.log(+bigIntVal); // TypeError: Cannot convert a BigInt value to a number
Although comparisons work with BigInt, what should you always remember?
View Answer:
console.log(1 == 1n); // true
console.log(1 === 1n); // false
Can you compare BigInt and Number using comparison operators?
View Answer:
let bigint = 1234567890123456789012345678901234567890n;
let number = 1234567890;
console.log(bigint > number); // Outputs: true
console.log(bigint < number); // Outputs: false
console.log(bigint === number); // Outputs: false
How does JavaScript handle equality checks between BigInt and Number?
View Answer:
For example...
let bigint = 123n;
let number = 123;
console.log(bigint === number); // Outputs: false
However, if you use the loose equality operator ==
, JavaScript will perform type coercion if the types of the operands are different. In the case of comparing a BigInt and a Number, this means the Number will be converted to a BigInt, and then the comparison will be done. So, a BigInt and a Number that are numerically equal will be considered equal by the ==
operator.
For example:
let bigint = 123n;
let number = 123;
console.log(bigint == number); // Outputs: true
It's important to note that using ==
can sometimes lead to unexpected results due to type coercion, so many developers prefer to always use ===
and handle type conversion explicitly when necessary.
Can you use BigInt with JSON.stringify?
View Answer:
let data = {
id: 9007199254740993n
};
console.log(JSON.stringify(data)); // Throws TypeError: Do not know how to serialize a BigInt
There are ways to work around this, though, such as by defining a toJSON
method on the object that converts BigInt values to strings or numbers. Here's an example of converting BigInt values to strings:
let data = {
id: 9007199254740993n,
toJSON() {
return {
id: this.id.toString()
};
}
};
console.log(JSON.stringify(data)); // Outputs: {"id":"9007199254740993"}
But please note that when you deserialize the JSON string, you'll have to convert the string back to a BigInt if needed. Also, remember that Number can only safely represent integers up to Number.MAX_SAFE_INTEGER
(9007199254740991
), so converting a BigInt to a Number can result in loss of precision if the BigInt is larger than that.