Loading...

What’s new in ES2023? List of new JavaScript Features

What’s new in ES2023? List of new JavaScript Features

It’s that exciting time of year again: TC39, the governing body for JavaScript standards, has frozen its ECMAScript 2023 proposal, signifying the upcoming release of ES2023. Today, we’ll delve into the new features that have achieved Stage 4 status and will be part of the finalized version.

1. Array.findLast() and Array.findLastIndex()

First up is the “Array find from last” feature, proposed by Wenlu Wang. It introduces two new methods for Array and TypedArray objects: findLast() and findLastIndex(). These methods function similarly to their existing counterparts (find() and findIndex()), but, as the names suggest, they start searching from the end of the array rather than the beginning. This feature provides a cleaner and more efficient way to perform reverse order searches, eliminating the need for manual array reversals or complex index calculations.

Let’s illustrate these new methods with a simple example:

const isEven = (number) => number % 2 === 0; const numbers = [1, 2, 3, 4]; // Traditional search (first to last) console.log(numbers.find(isEven)); // Output: 2 console.log(numbers.findIndex(isEven)); // Output: 1 // New reverse search (last to first) console.log(numbers.findLast(isEven)); // Output: 4 console.log(numbers.findLastIndex(isEven)); // Output: 3
Code language: JavaScript (javascript)

In this example, the findLast() and findLastIndex() methods locate the last even number in the array.

2. Hashbang Grammar

The second feature is the Hashbang Grammar, brought forth by Bradley Farias. It formalizes the usage of Hashbangs (also known as Shebangs), which are directives typically found at the beginning of Unix-based scripts that determine the interpreter for the script.

This update means that JavaScript can now standardly interpret scripts beginning with a hashbang, specifically in a Node.js environment. It essentially allows the execution of JavaScript scripts as standalone executables.

Here’s an example of how you can utilize this feature:

#!/usr/bin/env node console.log('Hello, World!');
Code language: JavaScript (javascript)

This JavaScript file can now be executed directly from a Unix-like command line, outputting “Hello, World!” to the console.

3. Symbols as WeakMap keys

The third major update, proposed by several contributors, allows Symbols to serve as keys in WeakMaps. Prior to ES2023, only Objects were permissible as WeakMap keys, despite Symbols also being unique and non-duplicable, making them ideal candidates.

This change allows for more flexibility in utilizing the memory-optimization benefits of WeakMaps and encourages a wider array of use-cases.

Here’s a demonstration of using Symbols as WeakMap keys:

const weak = new WeakMap(); const key = Symbol("ref"); weak.set(key, "Hello, ES2023!"); console.log(weak.get(key)); // Output: Hello, ES2023!
Code language: JavaScript (javascript)

4. Change Array by Copy

Last but certainly not least, the “Change Array by Copy” proposal by Ashley Claymore and Robin Ricard introduces several new methods that alter arrays while avoiding mutation of the original array. This is done by returning a new array copy with the desired modifications, promoting functional programming principles and enhancing code predictability.

The new methods include:

  • toReversed(): Returns a reversed copy of the array.
  • toSorted(): Returns a sorted copy of the array.
  • toSpliced(start, deleteCount, …items): Returns a spliced copy of the array.
  • with(index, value): Returns a copy of the array with the value at the specified index replaced.

A brief demonstration:

const original = [1, 2, 3, 4]; const reversed = original.toReversed(); console.log(original); // Output: [ 1, 2, 3, 4 ] console.log(reversed); // Output: [ 4, 3, 2, 1 ] const replaced = original.with(2, 99); console.log(original); // Output: [ 1, 2, 3, 4 ] console.log(replaced); // Output: [ 1, 2, 99, 4 ]
Code language: JavaScript (javascript)

These additions expand the versatility of array manipulations and bolster JavaScript’s commitment to providing tools that promote safer, more reliable programming.

ES2023 is packed with exciting enhancements that will further strengthen JavaScript’s standing as a versatile and reliable language. There’s much to look forward to, whether it’s improved array manipulation, standardized Hashbang usage, or expanded WeakMap keys. As always, keep experimenting, and happy coding!

Sharing is caring

Did you like what Mehul Mohan wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far