DEV Community

Cover image for New and Amazing JS Array methods from ES2023.
jeetvora331
jeetvora331

Posted on

New and Amazing JS Array methods from ES2023.

ES2023, also known as ECMAScript 14 (ES14), introduces new array copying methods that make JavaScript programs more predictable and maintainable. These methods, which include toSorted, toReversed, toSpliced, and with, allow you to perform operations on arrays without changing the data in place but by making a copy and changing that copy instead

Array.prototype.with()
The Array.prototype.with() method is the copying version of using bracket notation to set the value of an element at a given index in an array. Unlike bracket notation, it doesn't mutate the array it's called upon. It returns a new array, allowing you to chain array methods while performing manipulations without worrying about mutating the original array

For example, consider the following code snippet:

Image description

Without the .with() method, you would need to copy the array first, mutate the copy, and then sort the copy

Image description
The .with() method simplifies this process, allowing you to chain a call to the .sort() method:

Image description

The new array methods introduced in ES2023 make JavaScript programs more predictable and maintainable by allowing you to perform operations on arrays without changing the data in place. The with() method, in particular, simplifies the process of chaining array methods without worrying about mutating the original array. As browser support for these methods continues to grow, developers can look forward to more efficient and maintainable code.

Downside of these methods
While the ECMAScript 2023 spec is very new, there is already good support for these new array methods. Chrome 110, Safari 16.3, Node.js 20, and Deno 1.31 all support these methods and their corresponding polyfills and shims are available for platforms that don't yet have support

Top comments (1)

Collapse
 
pterpmnta profile image
Pedro Pimienta M.

Thanks for sharing