Introduction to ES2023

JavaScript has come a long way since its inception, and with ES2023, the 14th and current version, it’s about to get even better. Packed with a plethora of new features and enhancements, ES2023 aims to elevate your JavaScript development experience to new heights. Whether you’re a seasoned developer or just starting out, this version brings a host of improvements that will make your coding adventures more efficient, powerful, and enjoyable.

ES2023: Find the Array from the Last

.findLast()

  • The findLast() method operates by iterating through the array in reverse order and retrieves the value of the last element that meets the criteria defined by the provided testing function.
  • If no elements satisfy the condition, the method returns undefined.
ES2023 array.findLast()

.findLastIndex()

  • The findLastIndex() method performs a reverse iteration over the array and retrieves the index of the last element that meets the criteria specified by the provided testing function.
  • If no elements satisfy the condition, the method returns -1.
array.findLastIndex()

ES2023: Change Array by Copy

“Change Array by Copy” proposal brought forth by Ashley Claymore and Robin Ricard. This proposal introduces a set of new methods that allow for modifying arrays without changing the original array. Instead of mutating the original array, these methods return a new copy of the array with the desired modifications. This approach aligns with the principles of functional programming and enhances the predictability of the code.

ES2023: Array methods

.toReversed()

The toReversed() method takes the elements of the original array and rearranges them in reverse order. It then returns a new array containing these elements.

It’s important to note that the toReversed() method does not change the original array. Instead, it produces a new array with the reversed order of elements.

Using toReversed() can be helpful when you want to work with the elements of an array in the opposite order or need to manipulate the array without altering the original sequence.

.toSorted(compareFn)

The toSorted() method is a copying version of the sort() method available on arrays. It returns a new array with the elements sorted in ascending order.

ES2023: Array.toSorted()

The compareFn parameter is optional. It specifies a function that defines the sort order. If omitted, the elements are converted to strings and sorted based on their Unicode code point value.

.toSpliced(start,deleteCount,…items)

The toSpliced() method then returns a new array with the desired changes, while the original array remains unchanged.

It’s important to note that the toSpliced() method ensures that the resulting array is never sparse. If the original array had empty slots, the new array will have those slots filled with undefined values.

ES2023: Array.toSpliced()

.with(index,value)

The Array.prototype.with() method allows us to create a copy of an array and modify the value at a specific index without altering the original array. It follows a non-destructive approach, ensuring that the original array remains unchanged. This feature proves particularly useful when we want to perform chainable array operations while making specific modifications.

ES2023: Array.with()

By using the with() method, we can effortlessly create a modified version of an array without the need for complex copying or mutation techniques. It provides a concise and readable syntax, making our code more elegant and maintainable.

RangeError is thrown if the index is greater than or equal to the length of the array or less than the negative length of the array.

ES2023: Symbol as WeakMap Keys

In JavaScript, there is a data structure called WeakMap that allows us to associate values with keys. However, WeakMaps have a limitation: they only allow objects as keys. This is because objects in JavaScript have a unique identity aspect.

Symbols are a special type in JavaScript that allows us to create unique values. Unlike other primitive types like strings or numbers, each Symbol value is completely unique and different from any other Symbol value.

The proposal suggests using Symbols as keys in WeakMaps, instead of only allowing objects. This would enable us to use Symbols to associate values with keys in a WeakMap.

Symbols as WeakMap

Using Symbols as keys in WeakMaps allows us to take advantage of their uniqueness and use them to associate values with specific keys in a more efficient and expressive way. This can be particularly useful in various use cases, such as working with ShadowRealms, Record & Tuples, or any other scenario where unique key-value associations are needed.

ES2023: Hashbang Grammer

There are Command Line Interface (CLI) tools and environments that support the use of Shebangs or Hashbangs in JavaScript scripts. A Shebang or Hashbang is a special line at the beginning of a script that specifies the interpreter or command-line tool to use when executing the script.

In some of these CLI environments, the Hashbang line is removed from the script before it is passed to the JavaScript engine for execution. This removal is done to ensure that the script remains valid JavaScript code.

The proposal suggests a change in how this Hashbang removal process is handled. Instead of the CLI environments removing the Hashbang line, the proposal suggests that JavaScript engines themselves should be responsible for removing it. This would create a consistent approach across different JavaScript environments, ensuring that the behaviour of Hashbang removal is unified and standardized.

By moving the responsibility of Hashbang removal to the JavaScript engines, the proposal aims to simplify the process, make it more predictable, and ensure that the behaviour is consistent regardless of the CLI environment being used.