JS基本数据类型——BigInt

一、JS基本数据类型——BigInt

  BigIntES11引入的新的基本数据类型。BigInt数据类型的目的是比Number数据类型支持的范围更大的整数值,以任意精度表示整数。使用 BigInt解决了之前Number整数溢出的问题。

1.表示方式

1 //大整型
2 let n = 520n;//他只用在普通整型后边添加一个n就可以了
3 console.log(n,typeof(n));

2.BigInt函数

  可以将普通整数值转化为大整型的值。

1 //函数
2 let n = 123;
3 console.log(BigInt(n));
4 //不能使用浮点数进行转换
5 console.log(BigInt(0.2));

3.大数值运算

 1 let max = Number.MAX_SAFE_INTEGER;//Number的最大安全整数
 2 console.log(max);
 3 console.log(max + 1);
 4 //超过number的最大数值范围,运算就会出错
 5 console.log(max + 2);
 6 
 7 console.log(BigInt(max));
 8 //BigInt数据类型不能直接和普通数据类型进行运算
 9 console.log(BigInt(max) + BigInt(1));
10 console.log(BigInt(max) + BigInt(2));

posted @ 2022-12-11 16:40  雏栀  阅读(5442)  评论(0编辑  收藏  举报