DEV Community

V.D
V.D

Posted on • Originally published at javascript.plainenglish.io on

1

Future-Proof Your JavaScript Code

Future-Proof Your JavaScript Code: The Game-Changing Immutability Revolution

1

In this article, we will explore a set of new array methods that offer a fresh approach to achieving immutability.

The methods under discussion are with, toSorted, toReversed, and toSpliced.

Immutability ensures that the original array remains unchanged, and modifications are made on a new copy. While there were existing techniques for achieving immutability, such as utilizing the spread operator, these new array methods present more convenient alternatives.

These methods offer improved code readability and performance benefits, making them advantageous for developers.

  1. with() Method

The with() method of Array instances is the copying version of using the bracket notation to change the value of a given index. It returns a new array with the element at the given index replaced with the given value.

array.with(index, value) — will return a new array with the element at index replaced with value.

const arr = [1, 2, 3, 4, 5];
console.log(arr.with(2, 6)); // [1, 2, 6, 4, 5]
console.log(arr); // [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

2. toSpliced()

splice() — is method that will work on your original array, rather that on their copy.

For eg: — in below example, splice has replaced the start and end index with the new value i.e. ““mango””

what toSpliced() will do, it will not update the original array, rather than work on its copy and original array remains intact. It returns a new array instead of modifying the original array.

3. toSorted()

Normally, we do sort by adding sort() after any array, and it will sort the original array, there is a change in original array.

To remain intact with original array, you need to introduced spread operator and create a separate copy of that like this.

To remove this step, javascript has a new array method, there is no need to use the spread operator ie. toSorted()

4. toReversed()

This method is working same as array.reverse(), but rather than changing original array, it will create a new copy and original array remains intact.

Before using, any of these methods, please check their browser compatibility as many of them is not supported in Firefox, so make use if required. https://caniuse.com/?search=array.with

Also please make sure to check MDN Websiteto check for upcoming features in Javascript.

Thanks for reading


SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay