javascript基本类型BigInt

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
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值