JavaScript. What's new in ES2020?

by Silvia Mazzetta Date: 05-04-2020 javascript coding functions webdev ES2020

As we discussed in our article about the ES2019 features you should try, ECMAScript's proposals will continue to grow and give rise to new implementations.

Therefore, you can already access the new ECMAScript features summarized in ES2020. So, in order not to miss the train, it is worth to be informed about these features.

Let's do it!

Dynamic import

Dynamic importing is one of the most interesting features of ES2020. It is a feature that you can use with lazy loading.

Previously if you wanted to import a module in JavaScript you would have something like the following:

import Math from ('./Math.js');
const Math = new Math;
Math.multiply();

The problem we had in this case, is that it was a static way of importing the Math module, and we could not delay the load if we wanted this module to depend on a user action.

Thanks to ES2020, you can now use lazy loading without a webpack! From now on you can use import as a function anywhere in your code, using an asynchronous function that returns a Promise.

The above code in ES2020 could be written as follows:

import Math from ('./Math.js');
.then((Math) => {
  const Math = new Math;
  Math.multiply();
});

Supported browsers: Chrome 63, Firefox 67, Opera 50, Safari 11.1

BigInt

Representing a number greater than 2^53-1 in JavaScript can be a problem, as it is the largest number that JavaScript can safely represent using the Number object.

The BigInt object is a native object that helps you represent numbers greater than 2^53-1. BigInt is interesting, therefore, if, for example, you want to do a multiplication of two numbers and there is a possibility of overflow. As the name "Int" indicates, you cannot use BigInt with a floating number.

const bigNumber = BigInt (90073498284849)
//90073498284849n

Supported browsers: Chrome 67, Firefox 68, Opera 54

Promise.allSettled

The Promise.allSettled() method returns a promise that resolves after all promises have either been resolved or rejected, with a vector of objects describing each promise's results.

In fact, Promise.allSettled(), looks like Promise.all(). But, as you well remember, Promise.all() waits for all promises to be fulfilled, or for any promise to be rejected.

Example

const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve, reject)=>
setTimeout (reject, 100, 'foo'));
const promise = [promise1, promise2];

Promise.allSettled(promises).
 then((results)=> results.foreach((result)=>
 console.log(result.status)));

//"fulfilled"
//"rejected"

Supported browsers: Chrome 76, Firefox 71, Safari 13, Node 12.1

globalThis

The property globalThis is good news! globalThis contains the global value this according to the context of the global object.

Thanks to globalThis you don't have to differentiate if the code is running in a browser (this), or in Node (global).

Example:

function canMakeHTTPrequest(){
      return typeof globalThis.XMLHttpRequest === 'function';
}

console.log(canMakeHTTPrequest());
//output in a browser:true


Supported browsers: Chrome 71, Firefox 65, Safari 12.1, Node 12

Conclusion

ES2020 has many other new features, interesting to try and perfect to help you improve your code by reducing verbosity. However, as you can see from the features we share today, not all of them are supported by all browsers. Especially the older browsers.

However, it is always worth taking a look, and play and practice with them, until they become standard.

 
by Silvia Mazzetta Date: 05-04-2020 javascript coding functions webdev ES2020 hits : 9925  
 
Silvia Mazzetta

Silvia Mazzetta

Web Developer, Blogger, Creative Thinker, Social media enthusiast, Italian expat in Spain, mom of little 9 years old geek, founder of  @manoweb. A strong conceptual and creative thinker who has a keen interest in all things relate to the Internet. A technically savvy web developer, who has multiple  years of website design expertise behind her.  She turns conceptual ideas into highly creative visual digital products. 

 
 
 

Related Posts

How to upload files to the server using JavaScript

In this tutorial we are going to see how you can upload files to a server using Node.js using JavaScript, which is very common. For example, you might want to…

How to combine multiple objects in JavaScript

In JavaScript you can merge multiple objects in a variety of ways. The most commonly used methods are the spread operator ... and the Object.assign() function.   How to copy objects with…

The Payment Request API: Revolutionizing Online Payments (Part 2)

In the first part of this series, we explored the fundamentals of the Payment Request API and how it simplifies the payment experience. Now, let's delve deeper into advanced features…

The Payment Request API: Revolutionizing Online Payments (Part 1)

The Payment Request API has emerged as the new standard for online payments, transforming the way transactions are conducted on the internet. In this two-part series, we will delve into…

Let's create a Color Picker from scratch with HTML5 Canvas, Javascript and CSS3

HTML5 Canvas is a technology that allows developers to generate real-time graphics and animations using JavaScript. It provides a blank canvas on which graphical elements, such as lines, shapes, images…

How do you stop JavaScript execution for a while: sleep()

A sleep()function is a function that allows you to stop the execution of code for a certain amount of time. Using a function similar to this can be interesting for…

Mastering array sorting in JavaScript: a guide to the sort() function

In this article, I will explain the usage and potential of the sort() function in JavaScript.   What does the sort() function do?   The sort() function allows you to sort the elements of…

Infinite scrolling with native JavaScript using the Fetch API

I have long wanted to talk about how infinite scroll functionality can be implemented in a list of items that might be on any Web page. Infinite scroll is a technique…

Sorting elements with SortableJS and storing them in localStorage

SortableJS is a JavaScript extension that you will be able to use in your developments to offer your users the possibility to drag and drop elements in order to change…

What is a JWT token and how does it work?

JWT tokens are a standard used to create application access tokens, enabling user authentication in web applications. Specifically, it follows the RFC 7519 standard. What is a JWT token A JWT token…

Template Literals in JavaScript

Template literals, also known as template literals, appeared in JavaScript in its ES6 version, providing a new method of declaring strings using inverted quotes, offering several new and improved possibilities. About…

How to use the endsWith method in JavaScript

In this short tutorial, we are going to see what the endsWith method, introduced in JavaScript ES6, is and how it is used with strings in JavaScript. The endsWith method is…

Clicky