What’s new in modern JavaScript (part 2)

Tram Ho

  • 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 :

  • 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:

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

similar to the example below

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).

  • 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.

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.
Share the news now

Source : Viblo