ES2020: Summary of new features with examples π₯
In this article, weβre going to review some of the latest and greatest features coming with ES2020. π₯
π€ Getting started
We are going to test these features in a Node.js environment using Babel.
Method #1: creating a project from scratch
Start by creating a new project:
1 | $ mkdir es2020-tests |
Now add @babel/cli @babel/core @babel/node @babel/preset-env
dependencies:
1 | $ yarn add --dev @babel/cli @babel/core @babel/node @babel/preset-env |
Create a .babelrc
file:
1 | { |
Method #2: clone the source code repository
You can also clone the following GitHub repository that includes the setup and source code of the examples in this article.
1 | $ git clone git@github.com:olivierloverde/es2020-examples.git |
π€ The ES2020 features
Private variable in Class
You can now declare a private variable in a class by using a hastag #
. If a private variable is called outside of its class It will throw a SyntaxError
.
1 | class MyClass { |
BigInt
There was a limitation on largest possible integer because of how Javascript represented number internally (it is using a 64-bit floating point, see IEE 754.
1 | const maxInteger = Number.MAX_SAFE_INTEGER; |
Now there is a native solution, BigInt is a built-in object that provides a way to represent whole numbers larger than 2β΅Β³ β 1, which is the largest number in JS number).
You can create a BigInt by:
- creating a
BigInt
object:const value = new BigInt(500)
- appending a
n
to a number:const value = 500n
For the moment, it cannot be used with methods in the built-in Math
object and cannot be operated with Number
. Bitwise operators are supported except >>>
because all BigInts are signed.
1 | // Using BigInt |
Promise.allSettled()
Promise.allSettled takes an array of Promise
object as argument and waits that all promises settle to return the corresponding result as an array of objects {status, ?value, ?reason}
.
1 | const resolvingPromise1000ms = new Promise((resolve, reject) => setTimeout(resolve, 1000)); |
Nullish Coalescing Operator
When you use ||
operator, it returns the first argument to be true
. However, sometimes you a default value considered as false
such as 0
or ""
. To avoid it we can use the nullish coalescing operator ??
like below:
1 | let object = { |
Optional Chaining Operator
Letβs take the following object as an example:
1 | let person = { |
Letβs say we want to access a property on this object that we are not sure to have, we usually do:
1 | if (person.city !== undefined && person.city.locale !== undefined) { |
This ensures the program does not throw any βerror cannot read property name of undefinedβ.
Now with the optional chaining operator, we can be more concise:
1 | console.info(person?.city?.locale); |
Dynamic Import
Dynamic import()
returns a promise for the module namespace object of the requested module. Thus, we can now use the import()
function with the await
keyword and assign the module namespace object to a variable dynamically.
1 | const print = (value) => console.info(value); |
1 | const doPrint = async (value) => { |
String.prototype.matchAll
String.prototype.match
gives an array of all matches between a string and a regexp.
For example:
1 | const re = /(Mister )\w+/g; |
You are now ready to use these new ES2020 features! Please leave me comment if youβve liked it! π