Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS 2021

JavaScript Version Numbers

Earlier ECMAScript versions were named by numbers, such as ES5 and ES6. Starting in 2016, versions have been named by year, like ES2016, ES2018, ES2020, and so on.

New Features in ES2021

Warning

These features are relatively recent, so older browsers may require alternative code or a polyfill for compatibility.

JavaScript Promise.any()

Example

// Create a Promise
const myPromise1 = new Promise((resolve, reject) => {
  setTimeout(resolve, 200“King”);
});

// Create another Promise
const myPromise2 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100“Queen”);
});

// Run when any promise fulfill
Promise.any([myPromise1, myPromise2]).then((x) => {
  myDisplay(x);
});

Promise.any() has been supported in all modern browsers since September 2020.

js 11

JavaScript String ReplaceAll()

ES2021 added the replaceAll() method to strings.

Example

text = text.replaceAll(“Cats”,“Dogs”);
text = text.replaceAll(“cats”,“dogs”);

The replaceAll() method allows you to use a regular expression instead of a string for replacements. If a regular expression is provided, the global flag (g) must be set; otherwise, a TypeError will be thrown.

Example

text = text.replaceAll(/Cats/g,“Dogs”);
text = text.replaceAll(/cats/g,“dogs”);

JavaScript Numeric Separator (_)

ES2021 intoduced the numeric separator (_) to make numbers more readable:

Example

const num = 1_000_000_000;

The numeric separator is purely for visual clarity.

Example

const num1 = 1_000_000_000;
const num2 = 1000000000;
(num1 === num2);

The numeric separator can be inserted anywhere within a number.

Example

const num1 = 1_2_3_4_5;

Note: The numeric separator cannot appear at the beginning or end of a number.

In JavaScript, only variables can begin with an underscore (_).

The numeric separator has been supported in all modern browsers since January 2020.

js 12