Adopting new JavaScript syntax in Node.js 22
The formal specification for the JavaScript language is ECMAScript. New JavaScript features make their way into Node.js via updates to the underlying V8 JavaScript engine that the Node.js runtime is built on top of. ECMAScript has annual updates that provide new JavaScript language features and syntax.
New major versions of Node.js tend to include a significant upgrade to the V8 engine. Node.js version 22.0.0 was released with V8 version 12.4. However, the V8 version may be upgraded during the lifetime of Node.js 22.
Updated versions of V8 bring underlying performance improvements and new JavaScript language features and syntax to the Node.js runtime. This recipe will showcase a couple of the newer JavaScript language features that have been introduced in Node.js 22.
Getting ready
For this recipe, you will need to have Node.js 22 installed. You will also need to have access to a terminal.
How to do it…
In this recipe, we will be using the Node.js Read Eval Print Loop (REPL) to test out the newer JavaScript features that are available in Node.js 22. Follow these steps:
- First, let’s open the Node.js REPL. Enter the following command in your terminal:
$ node
- This should open the REPL, which is an interface that we can use to execute code. Expect to see the following output:
Figure 1.7 – Node.js REPL
- Start by entering the following command. This command will return the version of V8 that is embedded in the Node.js version you’re using:
> process.versions.v8 '12.4.254.21-node.19'
- Two new JavaScript
String
methods were made available since Node.js 20:String.prototype.isWellFormed
: This method returns whether the suppliedString
is well-formed UTF-16String.prototype.toWellFormed
: This method will replace unpaired surrogates with the replacement character (U+FFFD
), thus making the UTF-16 String well-formed.
You can demonstrate this in the REPL:
> "006E006F00640065".isWellFormed() true
- Another recent feature addition is the
Intl.NumberFormat
built-in object, which provides language-based number formatting. Let’s test this out. In the REPL, declare a number:> const number = 123456.789; undefined
- Next, let’s format that number as if it were the Great British Pound (GBP):
> new Intl.NumberFormat('en-UK', { style: 'currency', currency: 'GBP' }).format(number); '£123,456.79'
- In Node.js 22, new
Set
methods likeunion
,intersection
, anddifference
were added as part of the V8 12.4 update. These enhancements make it easier to perform operations on numeric sets. Here’s an example involving prime and odd numbers:> const oddNumbers = new Set([1, 3, 5, 7]), primeNumbers = new Set([2, 3, 5, 7]); undefined > console.log('All Numbers:', [...(oddNumbers.union(primeNumbers))].toString()); All Numbers: 1,3,5,7,2 undefined > console.log('Common Numbers:', [...(oddNumbers.intersection(primeNumbers))].toString()); Common Numbers: 3,5,7 undefined > console.log('Exclusive Primes:', [...(primeNumbers.difference(oddNumbers))].toString()); Exclusive Primes: 2
Using the REPL, we’ve explored a couple of the new JavaScript language features that are available in Node.js 22. The main goal of this learning is that new JavaScript language features become available through upgrading the underlying Google Chrome V8 engine.
How it works…
New JavaScript language features are introduced into Node.js via updates to the underlying Google Chrome V8 JavaScript engine. A JavaScript engine parses and executes JavaScript code. The embedding of the Google Chrome V8 engine in Node.js is what enabled the execution of JavaScript outside of the browser. Chrome’s V8 JavaScript engine is one of many available JavaScript engines, with Mozilla’s SpiderMonkey, which is used in the Mozilla Firefox browser, being another leading JavaScript engine.
Every 6 weeks, a new version of Google Chrome’s V8 engine is released. Node.js 22 will continue to incorporate updates into V8, provided they can be made application binary interface (ABI)-compatible. An ABI describes how programs can interact with functions and data structures via compiled programs. It can be considered similar to a compiled version of an application programming interface (API).
Once there is a release of V8 that no longer allows ABI compatibility, the specific release line of Node.js will be fixed on that version of V8. However, specific V8 patches and fixes may continue to be applied directly to that Node.js release line. Node.js 20 is now fixed on V8 version 11.3, whereas Node.js 22, at the time of writing, is at V8 12.4. The V8 version in Node.js 22 will continue to be updated until ABI compatibility of newer versions of V8 can no longer be maintained.
The V8 JavaScript engine compiles JavaScript internally using just-in-time (JIT) compilation. JIT compilation speeds up the execution of JavaScript. While V8 is executing JavaScript, it obtains data about the code that is being executed. From this data, the V8 engine can make speculative optimizations. Speculative optimizations anticipate the upcoming code based on the code that has recently been executed. This allows the V8 engine to optimize for the upcoming code.
The V8 blog provides announcements of new V8 releases and details the new features and updates to V8. The V8 blog can be accessed at https://v8.dev/blog.