ECMAScript 2021
JavaScript Version Numbers
Earlier versions of ECMAScript were identified by numbers, such as ES5 and ES6.
Starting in 2016, the versions are labeled according to the year they were released: ES2016, 2018, 2020, and so on.
New Features in ES2021
Warning
These characteristics are quite recent.
Some older web browsers might require a different code, known as a Polyfill.
JavaScript Promise.any()
JavaScript String ReplaceAll()
ES2021 added a new string function called replaceAll()
.
The replaceAll()
method lets you use a regular expression instead of a string for replacement.
If you use a regular expression as the parameter, make sure to set the global flag (g). If not, it will result in a TypeError.
Note
ES2020 brought in a new string method called matchAll().
JavaScript Numeric Separator (_)
ES2021 added a numeric separator (_) to enhance the readability of numbers.
The number separator is just for looks.
You can put the numeric separator anywhere in a number.
Note
Numeric separators should not be placed at the start or end of a number.
In JavaScript, only variables are allowed to begin with an underscore (_).
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Promise Object</h1>
<h2>The Promise.any() Method</h2>
<p id="demo"></p>
<script>
// 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 fulfills
Promise.any([myPromise1, myPromise2]).then((x) => {
myDisplay(x);
});
// Funtion to run when a Promise is settled:
function myDisplay(some) {
document.getElementById("demo").innerHTML += some;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The replaceAll() Method</h2>
<p>ES2021 intoduced the string method replaceAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
text = text.replaceAll("Cats", "Dogs");
text = text.replaceAll("cats", "dogs");
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The replaceAll() Method</h2>
<p>ES2021 intoduced the string method replaceAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular";
text = text.replaceAll(/Cats/g, "Dogs");
text = text.replaceAll(/cats/g, "dogs");
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<p>ES2021 intoduced the numeric separator (_) to make numbers more readable:</p>
<p id="demo"></p>
<script>
const num = 1_000_000_000;
document.getElementById("demo").innerHTML = num;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<p>ES2021 brought in a new way to make numbers easier to read: the numeric separator (_).</p>
<p>Is 1_000_000_000 the same as 1000000000?</p>
<p id="demo"></p>
<script>
const num1 = 1_000_000_000;
const num2 = 1000000000;
document.getElementById("demo").innerHTML = (num1 === num2);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<p>ES2021 brought in a new way to make numbers easier to read: the numeric separator (_).</p>
<p id="demo"></p>
<script>
const num = 1_2_3_4_5;
document.getElementById("demo").innerHTML = num;
</script>
</body>
</html>