|
|
|
|
|
by mediumdeviation
3230 days ago
|
|
Updating an array element arr[3] = 'New content';
Updating an array element immutably arr = arr.map((element, index) => {
if (index === 3) return 'New content';
return element;
});
It's possible, sure, but it feels terrible. JavaScript objects want to be mutated - it's the most idiomatic way of interacting with them. Whether functional programming paradigm is good (I certainly feel it is) is beside the point - JS wasn't designed for it.When I think of immutable primitives, I think of Python's tuples, which will raise a big fat error when you try to mutate them, or even better, Swift's structs, which gives you a new copy with every mutation. |
|
Between the spread operator (...), immutable array methods (slice, reduce, map, filter, and even sort if preceded by .slice()), immutability in native JS ain't half bad.