JavaScript 2021
JavaScript Basics

JavaScript 2021

JS 2021

ECMAScript 2021 Overview

JavaScript has evolved significantly since its early versions. Initially, ECMAScript versions were identified numerically like ES5 and ES6. Starting in 2016, the naming convention shifted to include the year of release, leading to names like ES2016, ES2018, ES2020, and so on.

New Features in ES2021

  • Promise.any(): Aims to resolve with the value from the first fulfilled promise in a list of promises.
  • String replaceAll(): Enhances string manipulation by allowing global replacements within a string.
  • Numeric Separators (_): Improves readability of large numeric literals by allowing underscores as separators.

Code Examples and Browser Compatibility

Promise.any()

Resolves as soon as any of the supplied promises fulfills:

const promise1 = new Promise((resolve, reject) => {  
setTimeout(resolve, 200, "First");});
const promise2 = new Promise((resolve, reject) => {  
setTimeout(resolve, 100, "Second");});
Promise.any([promise1, promise2]).then(result => console.log(result));‍

Available in browsers like Chrome 85 and Firefox 79 from mid-2020.

String replaceAll()

Facilitates replacing all instances of a substring or pattern:

let text = "Cats are playful, cats are fun!";
text = text.replaceAll("cats", "dogs"); // Case-sensitive replacementtext =
text.replaceAll(/cats/gi, "dogs"); // Case-insensitive replacement using regex

This method is included in major browsers as of ES2021.

Numeric Separator (_)

Allows underscores in numeric literals to improve readability:

const readableNumber = 1_000_000_000; // One billion
console.log(readableNumber === 1000000000); // true, underscores are purely visual

The numeric separator is recognized by modern browsers like Chrome 75 and Safari 13.1 from early 2020.

These enhancements in ES2021 provide developers with more tools for writing clear and efficient JavaScript code, though care should be taken to ensure compatibility with older browsers through polyfills where necessary.

Take a look into your desired course