- JavaScript has evolved very rapidly in recent years. Especially after the release of ES6 in 2015, things have turned out great.
- In this article, we will discuss about the features introduced in ES2020 version.
The optional chaining operator
- The optional chaining operator: Provides a way to simplify accessing values through objects
- Allow to handle short-circuiting if the reference is null or undefined, it returns undefined instead of throwing an error from which the program cannot continue.
- This makes expressions shorter and simpler when accessing object properties where the viability of the reference is not guaranteed.
See example below: we have 1 object is blog :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ``` const blog = { title: "My Blog", author: { name: "Yen", links: { website: "https://viblo.asia/", }, }, }; const linkToWebsite = blog && blog.author && blog.author.links && blog.author.links.website; ``` |
- By using the? operator, JavaScript knows to implicitly check to make sure references are between null or undefined before attempting to access further
- If the reference is null or undefined, the expression will automatically stop and return undefined. Here is the same example using the option string:
1 2 3 4 5 | ``` //ES2020 const linkToWebsite = blog?.author?.links?.website; ``` |
The Nullish Coalescing (??) operator
- The Nullish Coalescing (??) operator is a logical operator that returns its right operand when its left operand is null or undefined, and vice versa.
Results of a ?? b is:
Select a if it is not null or undefined Select b for the opposite above
1 2 3 4 5 6 7 8 | ``` const res = (a !== null && a !== undefined) ? a : b; //ES2020 const res = a??b; ``` |
similar to the example below
1 2 3 4 5 6 7 8 | ``` let firstName = null; let lastName = null; let nickName = "Yen xinh gai"; console.log(firstName ?? lastName ?? nickName ?? "Anonymous"); // nếu firstName null hoặc undefine thì lấy lastName. nếu lastName null thì chọn nickname nấu nickName null thì là 'Anonymous' ở đây first và last đều null nên kết quả sẽ là Yen xinh gai ``` |
Dynamic Imports
- Import allows us to import functions from other modules
- To dynamically import a module, we can be called a function and automatically return Promise, Await
// ES2020 import (‘/ modules / my-module.js’) .then ((module) => {// Do something with the module.});
// ES2020 let module = await import (‘/ modules / my-module.js’);
BigInt
- Integer in Javascript is limited to 2 ^ 53- 1
- Number.MAX_SAFE_INTEGER represents the maximum safe integer in JavaScript (2 ^ 53 – 1).
1 2 3 4 5 6 7 8 9 10 | const x = Number.MAX_SAFE_INTEGER + 1; const y = Number.MAX_SAFE_INTEGER + 2; console.log(Number.MAX_SAFE_INTEGER); // expected output: 9007199254740991 console.log(x); // expected output: 9007199254740992 |
- To process many times larger values with high performance, we can use the following Big Integer library.
- Big integers allow to perform addition, subtraction, multiplication, division, comparison, … with unlimited integers, as long as you have enough RAM.
1 2 3 4 5 6 7 8 | const previouslyMaxSafeInteger = 9007199254740991n const alsoHuge = BigInt(9007199254740991) // ↪ 9007199254740991n const hugeString = BigInt("9007199254740991") // ↪ 9007199254740991n |
Promise.allSettled ()
- The Promise.allSettled () method will return an array of all the existing Promises that were made or rejected.
- It is often used when you have many asynchronous tasks that do not depend on each other for successful completion or you always want to know the result of each promise.123456789101112//ES2020const promise1 = Promise.resolve(3);const promise2 = new Promise((resolve, reject) => setTimeout(reject, 100, 'Something went wrong'));const promises = [promise1, promise2];Promise.allSettled(promises).then((results) => results.forEach((result) => console.log(result.status)));// expected output:// "fulfilled"// "rejected"