1.BigInt用来表示大于2**53 - 1的整数
js中Number类型只能表示2**53 - 1内的整数的运算
console.log(2**53)
//9007199254740992
console.log(2**53 + 1);
//9007199254740992
console.log(BigInt(2**53) + BigInt(1))
//9007199254740993n
console.log((BigInt(2**53) + BigInt(1)).toString())
//9007199254740993
2.BigInt不能用于Math对象中的方法,BigInt只能使用+、*、-、**、%。除>>>之外的位操作符也支持。
3.BigInt不能跟Number实例进行混合运算,但BigInt可以被Number比较,两者也可以混在一个数组内并排序。
console.log(1 + 4n)
//Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
console.log(1 == 1n);
//true
console.log(1 === 1n);
//false
console.log(typeof 1n === 'bigint')
//true
console.log([4n, 6, -12n, 10, 4, 0, 0n].sort());
// [-12n, 0, 0n, 10, 4n, 4, 6]
4.BigInt带小数的运算会被向下取整
console.log(5n/3n)
//1n, not 1.6667n
console.log(5n/2n)
//1n, not 2.5n
5.需要转换成Boolean时的表现跟Number类似
0n || 12n;
//12n
Boolean(0n);
//false
Boolean(12n);
//true