How to feature test for BigInt support?

166 Views Asked by At

I'm not familiar with modern JS and tooling to even try something.

References:

https://github.com/tc39/proposal-bigint

4

There are 4 best solutions below

0
On BEST ANSWER

Simply invoke BigInt and catch a possible exception:

let BigIntSupported = true

try {
    BigInt(1)
} catch (e) {
    BigIntSupported = false

}
0
On

globalThis.BigInt === BigInt without any polyfill.

console.log(globalThis.BigInt === BigInt);
    

I did just 2 tests with Firefox 119.1.1 and it seems to work... Second test had intentional typo and failed as expected.

References:

https://github.com/tc39/proposal-global

I can't edit my older answer for some reason...

2
On

if('BigInt' in window) (or if(window.BigInt)) should suffice.

2
On

globalThis.BigInt === BigInt

core-js's polyfill requires Chrome 71 or Firefox 65.

References:

https://github.com/tc39/proposal-global

http://zloirock.github.io/core-js/compat/