What's new in ES2020
What’s new in ES2020
ES2020 includes many new interesting changes and we are going to cover them in this lesson.
Not all browsers currently support these features so, I recommend you use the latest version of Chrome or Firefox to test the code examples. Otherwise, if you want to use them in your project, be sure to install a compiler like Babel, which at their latest version 7.8 already supports ES2020 by default so you don’t need to use any plugin.
BigInt
The support for BigInt
means that we will be able store much larger integers in our JavaScript
. The current max is 2^53 and you can get it by using Number.MAX_SAFE_INTEGER
. That does not mean that you cannot store larger integer, but JavaScript
does not handle them well, let’s look at an example:
let num = Number.MAX_SAFE_INTEGERconsole.log(num);// 9007199254740991console.log(num + 1);// 9007199254740992console.log(num + 2);// 9007199254740992console.log(num + 3);// 9007199254740994console.log(num + 4);// 9007199254740996
As you can see, once we hit the max that JavaScript
can handle, things stop working as we think they should.
In order to start using BigInt
, we can use the constructor BigInt
or we can simply append an n
at the end of your long integer and everything will continue working smoothly as it should.
// IF YOU ARE GETTING ERRORS, RUN THIS CODE IN THE CONSOLE OF YOUR BROWSER// let bigInt = BigInt(99999999999999999);let bigInt = 99999999999999999n;console.log(bigInt + 1n);// 100000000000000000n
As you can see I did not add 1
but I added 1n
, that’s because ...