2022-06

Ecma International approves ECMAScript 2022: What’s new?

[2022-06-22] dev, javascript, es2022

On 22 June 2022, the 123nd Ecma General Assembly approved the ECMAScript 2022 language specification, which means that it’s officially a standard now.

This blog post explains what’s new.

Alternatives to installing npm packages globally

[2022-06-18] dev, javascript, nodejs

There are two ways in which npm packages can be installed:

  • Locally, into a node_modules directory that npm searches for (or creates) in the current directory and its ancestors:

    npm install some-package
    
  • Globally, into a global node_modules directory:

    npm install --global some-package
    

    (Instead of the long version --global of this flag, we can also use the shorter -g.)

The latter requires root access on macOS and some other Unix platforms – which is a considerable downside. That’s why this blog post explores alternatives to global installs.

Using web streams on Node.js

[2022-06-17] dev, javascript, nodejs

Web streams are a standard for streams that is now supported on all major web platforms: web browsers, Node.js, and Deno. (Streams are an abstraction for reading and writing data sequentially in small pieces from all kinds of sources – files, data hosted on servers, etc.)

For example, the global function fetch() (which downloads online resources) asynchronously returns a Response which has a property .body with a web stream.

This blog post covers web streams on Node.js, but most of what we learn applies to all web platforms that support them.

Running Windows/ARM on Apple Silicon Macs via UTM

[2022-06-04] dev, javascript, nodejs

UTM is a free virtualization software that runs Windows/ARM on Apple Silicon Macs. This blog post explains how to use it.

2022-05

Processing Arrays non-destructively: for-of vs. .reduce() vs. .flatMap()

[2022-05-26] dev, javascript

In this blog post, we look at three ways of processing Arrays:

  • The for-of loop
  • The Array method .reduce()
  • The Array method .flatMap()

The goal is to help you choose between these features whenever you need to process Arrays. In case you don’t know .reduce() and .flatMap() yet, they will both be explained to you.

In order to get a better feeling for how these three features work, we use each of them to implement the following functionality:

  • Filtering an input Array to produce an output Array
  • Mapping each input Array element to one output Array element
  • Expanding each input Array element to zero or more output Array elements
  • Filter-mapping (filtering and mapping in one step)
  • Computing a summary for an Array
  • Finding an Array element
  • Checking a condition for all Array elements

Everything we do is non-destructive: The input Array is never changed. If the output is an Array, it is always freshly created.

RFC 9239: Updates to ECMAScript media types

[2022-05-18] dev, javascript

(This blog post is based on a tweet thread and additional input by Mathias Bynens.)

After work started on it in August 2017, May 2022 finally saw the publication of RFC 9239 “Updates to ECMAScript media types” by Matthew A. Miller, Myles Borins, Mathias Bynens, and Bradley Farias. It updates JavaScript MIME type registrations to align with reality:

  • The JavaScript MIME type is now unambiguously text/javascript.
  • .mjs is now a registered filename extension, specifically for JavaScript modules.

This unblocks tooling and server software like Apache to support JavaScript modules out of the box in a unified manner, like e.g. Node.js already does. Better industry alignment on MIME type and file extensions increases interoperability across tooling and other software.

The triple dot syntax (...) in JavaScript: rest vs. spread

[2022-05-04] dev, javascript

In JavaScript, the same syntax – triple dots (...) – is used for two different mechanisms:

  • Rest syntax is for receiving data.
  • Spreading is for sending data.

This blog post examines how these mechanisms work and why they are not operators.

2022-04

ECMAScript proposal “Change Array by copy”: four new non-destructive Array methods

[2022-04-10] dev, javascript, es proposal

This blog post describes the ECMAScript proposal “Change Array by copy” by Robin Ricard and Ashley Claymore. It proposes four new methods for Arrays and Typed Arrays:

  • .toReversed()
  • .toSorted()
  • .toSpliced()
  • .with()

ShadowRealms – an ECMAScript proposal for a better eval()

[2022-04-04] dev, javascript, es proposal

This blog post describes the ECMAScript proposal “ShadowRealm API” by Dave Herman, Caridy Patiño, Mark S. Miller, Leo Balter, and Rick Waldron.

Class ShadowRealm provides a new way of evaluating code at runtime – think eval() but better:

  • Each instance has its own global JavaScript scope.
  • Code is evaluated in that scope. If it changes global data, that only affects the ShadowRealm, but not the real global data.

2022-03

ECMAScript proposal: searching Arrays from end to start via .findLast() and .findLastIndex()

[2022-03-24] dev, javascript, es proposal

This blog post describes the ECMAScript proposal “Array find from last” by Wenlu Wang and Daniel Rosenwasser.