DEV Community

Mushfiqur Rahman Shishir
Mushfiqur Rahman Shishir

Posted on • Updated on

Top New JavaScript Features Introduced in ES2020

Since the addition of ECMAScript in 2015, the overall state of the art of the development in JavaScript has emerged to a new height. The JavaScript language specification has been regularly revised with new features every year. The ECMA International is calling these features new ECMAScript 2020 or JavaScript ES11(see intro here). In this post, we are going to discuss a few new interesting features of those.

LET’S DIVE INTO THE NEW JAVASCRIPT FEATURES

String.prototype.matchAll()

Though this function isn’t exactly a brand new one, since the new update in ES11, we are having a very good time when matching string against a regular expression. Let’s look at the following example:

const regexp = RegExp('foo[a-z]*','g');
const str = 'table football, foosball';
const matches = str.matchAll(regexp);

for (const match of matches) {
  console.log(`Found ${match[0]} 
           start=>${match.index} end=>${match.index + match[0].length}.`);
}
// expected output: "Found football start=>6 end=>14."
// expected output: "Found foosball start=>16 end=>24."

// matches iterator is exhausted after the for..of iteration
// Call matchAll again to create a new iterator
Array.from(str.matchAll(regexp), m => m[0]);
// Array [ "football", "foosball" ]
Enter fullscreen mode Exit fullscreen mode

Dynamic Imports with import()

Dynamic imports in JavaScript natively give you the option to import JavaScript files dynamically as modules in your application. This is just like how you do it with Webpack and Babel at the moment. This feature will help you ship on-demand-request code, better known as code splitting, without the overhead of Webpack or other module bundlers. You can also conditionally load code in an if-else block if you like.

The good thing is that you actually import a module, and so it never pollutes the global namespace.

const doMath = async (num1, num2) => {
  if (num1 && num2) {
    const math = await import('./math.js');
    console.log(math.add(5, 10));
  };
};
doMath(4, 2);
Enter fullscreen mode Exit fullscreen mode

BigInt

BigInt is one of the most anticipated features in JavaScript which is now finally here. It actually allows developers to have much greater integer representation in their JS code for data processing for data handling.

At the moment the maximum number you can store as an integer in JavaScript is pow(2, 53) - 1. But BigInt actually allows you to go even beyond that.

let oldNum = Number.MAX_SAFE_INTEGER; 
console.log(oldNum);
//output => 9007199254740991

let newNum = 9007199254740992n;
console.log(newNum);
//output => 9007199254740992n

++oldNum
console.log(oldNum);
//output => 9007199254740992

++oldNum
console.log(oldNum);
//output => 9007199254740992

++newNum
console.log(newNum);
//output => 9007199254740993n

++newNum
console.log(newNum);
//output => 9007199254740994n

++newNum
console.log(newNum);
//output => 9007199254740995n
Enter fullscreen mode Exit fullscreen mode

Nullish Coalescing

Nullish coalescing adds the ability to truly check nullish values instead of falsey values. What is the difference between nullish and falsey values, you might ask? In JavaScript, a lot of values are falsey, like: empty strings, the number 0, undefined, null, false, NaN, etc.

Probably, there’s a lot of times you might want to check if a variable is nullish, that is if it is either undefined or null, like when it’s okay for a variable to have an empty string, or even a false value.

In that case, you’ll use the new nullish coalescing operator -> ??

false ?? 'Something truthy'
false

undefined ?? 'Something truthy'
"Something truthy"

null ?? 'Something truthy'
"Something truthy"

false || 'Something truthy'
"Something truthy"

undefined || 'Something truthy'
"Something truthy"

null || 'Something truthy'
"Something truthy"

NaN ?? 'Something truthy'
NaN

NaN || 'Something truthy'
"Something truthy"
Enter fullscreen mode Exit fullscreen mode

Read The Full Article.

Thanks for reading and please do share if you like it.

Top comments (0)