Skip to main content

39 posts tagged with "ECMAScript"

View All Tags

Class static initialization blocks

· 2 min read
Shu-yu Guo ([@_shu](https://twitter.com/_shu))

The new class static initialization block syntax lets developers gather code that should run once for a given class definition and put them in a single place. Consider the following example where a pseudo-random number generator uses a static block to initialize an entropy pool once, when the class MyPRNG definition is evaluated.

RegExp match indices

· 5 min read
Maya Armyanova ([@Zmayski](https://twitter.com/Zmayski)), regularly expressing new features

JavaScript is now equipped with a new regular expression enhancement, called “match indices”. Imagine you want to find invalid variable names in JavaScript code that coincide with reserved words, and output a caret and an “underline” under the variable name, like:

`String.prototype.replaceAll`

· 4 min read
Mathias Bynens ([@mathias](https://twitter.com/mathias))

If you’ve ever dealt with strings in JavaScript, chances are you came across the String#replace method. String.prototype.replace(searchValue, replacement) returns a string with some matches replaced, based on the parameters you specify:

Top-level `await`

· 5 min read
Myles Borins ([@MylesBorins](https://twitter.com/MylesBorins))

Top-level await enables developers to use the await keyword outside of async functions. It acts like a big async function causing other modules who import them to wait before they start evaluating their body.

Nullish coalescing

· 7 min read
Justin Ridgewell

The nullish coalescing proposal (??) adds a new short-circuiting operator meant to handle default values.

You might already be familiar with the other short-circuiting operators && and ||. Both of these operators handle “truthy” and “falsy” values. Imagine the code sample lhs && rhs. If lhs (read, left-hand side) is falsy, the expression evaluates to lhs. Otherwise, it evaluates to rhs (read, right-hand side). The opposite is true for the code sample lhs || rhs. If lhs is truthy, the expression evaluates to lhs. Otherwise, it evaluates to rhs.

Optional chaining

· 5 min read
Maya Armyanova ([@Zmayski](https://twitter.com/Zmayski)), breaker of optional chains

Long chains of property accesses in JavaScript can be error-prone, as any of them might evaluate to null or undefined (also known as “nullish” values). Checking for property existence on each step easily turns into a deeply-nested structure of if-statements or a long if-condition replicating the property access chain:

`globalThis`

· 2 min read
Mathias Bynens ([@mathias](https://twitter.com/mathias))

If you’ve written JavaScript for use in a web browser before, you may have used window to access the global this. In Node.js, you may have used global. If you’ve written code that must work in either environment, you may have detected which of these is available, and then used that — but the list of identifiers to check grows with the number of environments and use cases you want to support. It gets out of hand quickly: