JavaScript array methods offer plenty to web developers already. The methods like map(), reduce(), filter() , some(), every(), and many more have helped build our JavaScript muscles for better logic and algorithms.
The with() method is a relatively n…
JavaScript array methods offer plenty to web developers already. The methods like map(), reduce(), filter() , some(), every(), and many more have helped build our
JavaScript muscles for better logic and algorithms.
The with() method is a relatively new inclusion to the JavaScript array method list, and its only intention is to change the value of a given index in an array in the “immutable” way. A big deal? Yes! Let’s learn.
Btw, please do not confuse the with() method from the JavaScript Array with the with statement from the language. The
with statement has been deprecated whereas, the with() method is a new inclusion to the language. People say JavaScript is weird, naming things is possibly a reason for it!
This article is also available as a video tutorial now. You can check it out here:
How would you change the element of an array for a given index? Suppose you need to change the element 3 which is at the index 2(remember, the array index in JavaScript starts with 0) with a new element, say, 6.
This works! However, there is a problem. We have mutated(aka, permanently changed) the original array. When you deal with data in your application, a data structure like an array should hold the original(unchanged) data as a “source of truth”. You wouldn’t want it to be modified by any functions and logic to achieve better predictability in your code.
Immutability is a mechanism to ensure that we can not change something(an array, object, etc). However, if we need a modified version of it, we need to first create a copy of the original and then make modifications to the copied version. This ensures that the original data is never changed by anything in the application, knowingly or unknowingly.
So, how about changing the element of an array at a given index, in an immutable way? It means every time we change an element in an array, we do not mutate the original array, rather we get back a new copy of the original array with the change applied.
ECMAScript 2023 introduced a new method called with() to solve this problem of mutability and achieve immutability while changing an element of an array. So now, instead of using a given index on the array directly, we can use the with() method to change the element value.
The with() method takes two parameters:
index - An index value which can start from 0, and also can be a negative number. The negative index counts backwards from the end of the array.
value - The value to change at a given index.
Let us now apply the with() method on the same array to change the element at a given index,
One more drawback of the direct index-based element seeking to get an element at a given index is that you can not use the negative number as an index value. Try something like the code snippet below, you will get an undefined.
Did you see that? You have ended up adding something completely different than what you anticipated. But, using the with() method, you can change an element of an array at a negative index. The negative index starts from the end of the array with an index value of -1 and move backwards.
As the with() method returns an array, you can chain the output of the with() method along with other array methods, like map(), filter(), reduce(), etc.
In the code snippet below, we replace the second element of the ages array with a new element, and then map the output array to multiply each element by four.
Can you please give a real-life use case of the with() method?
#
Sure! When I published a
YouTube video on this topic, I got a question about the real-life use case of the with() method. The exciting thing is, that I discovered the with() method when I was building a use case that needs the feature it offers. The image below provides a summary of the use case.
That’s all for now. I hope you agree with me that the with() method is a true gem providing us immutability and negative index accessibility with JavaScript Arrays. You can check out these articles to learn more about JavaScript arrays and their unique use cases: