8-2 新的原始数据类型:BigInt

24 阅读1分钟

number 的取值范围最大是 9007199254740992,不能输出比最大值更大的数字

const max = 2 ** 53
console.log(max) // 9007199254740992

console.log(Number.MAX_SAFE_INTEGER)
console.log(max === max + 1) // true
const bigInt = 9007199254740993
console.log(bigInt) // 9007199254740992 

不能输出比最大值更大的数字

需要在结尾加一个n

const bigInt = 9007199254740993n
console.log(bigInt) // 9007199254740993n
console.log(typeof bigInt)  // bigint

console.log(1n == 1) // true
console.log(1n === 1) //false
const bigint2 = BigInt(9007199254740993n)
console.log(bigint2) // 9007199254740993n
const num = bigInt + bigint2
console.log(num.toString())

number类型存储相加已经超过最大值,可以使用字符串的方式存储

bigint 存储方式,定义的时候在数字后面加一个n就可以了