The opposite view of immutability
Short answer: immutability is more a fashion trend than a necessity for JavaScript. There are a few narrow cases where this becomes useful (mostly React / Redux), although usually for the wrong reasons.
Long answer: read below.
Why is immutability so important (or necessary) in JavaScript?
Well, I'm glad you asked!
Some time ago, a very talented guy named Dan Abramov wrote a javascript state management library called Redux that uses pure functions and immutability. He also made some really cool videos that made the idea very easy to understand (and sell).
The time was perfect. The Angular novelty was disappearing, and the JavaScript world was ready to fixate on the latest innovations with the proper degree of steepness, and this library was not only innovative, but also perfect for React, which is being sold by another power station in Silicon Valley .
Sadly, fashion rules the world of JavaScript. Now Abramov is being proclaimed a demigod, and all of us, mere mortals, are forced to obey the Tao of Invariability ... does this make sense or not.
What is wrong with mutating objects?
Nothing!
In fact, programmers mutated objects forever ... as long as there were objects that needed to be mutated. 50+ years of application development, in other words.
And why complicate things? When you have a cat object and it dies, do you really need a second cat to track the changes? Most people will just say cat.isDead = true and cat.isDead = true with this.
Don't (mutating objects) make things simple?
YES! .. Of course it is!
Especially in JavaScript, which in practice is most useful for displaying some state that is supported elsewhere (for example, in a database).
What if I have a new news item that needs to be updated? ... How do I achieve this? Delete the store and recreate it? Is adding an object to an array a less expensive operation?
Well, you can follow the traditional approach and update the News object, so that your representation of this object in memory will change (and the representation displayed to the user, or one might hope so) ...
Or alternatively ...
You can try the "FP / Immutability" approach and add your changes to the News object in an array that tracks each historical change, so you can then iterate over the array and figure out what the correct state view should be (yuck!).
I'm trying to find out what is right here. Please enlighten me :)
Fashion comes and goes, buddy. There are many ways to skin a cat.
I'm sorry that you have to confuse the ever-changing set of programming paradigms. But hello, welcome to the club!
Now there are a few important points to remember regarding immutability, and you will get these throws at you with a feverish intensity that only naivety can show.
1) Consistency is exceptional because it avoids race conditions in multi-threaded environments .
Multithreaded environments (such as C ++, Java, and C #) are guilty of the practice of locking objects when more than one thread wants to change them. This has a bad effect on performance, but is better than an alternative to data corruption. And still not as good as making everything unchanged (praise to Lord Haskell!).
BUT Alas! In JavaScript, you always work with a single thread . Even web workers (each working in a separate context ). Since you cannot have a race condition associated with a thread inside the execution context (all these nice global variables and closures), the main thing in favor of immutability goes beyond.
(Having said that, there is the advantage of using pure functions in web workers, which is that you will not have any expectations about manipulating objects in the main thread.)
2) Consistency can (somehow) escape the race condition in the state of your application.
And this is the essence of the matter, most developers (React) will tell you that immutability and FP can somehow use this magic, which allows the state of your application to become predictable.
Of course, this does not mean that you can avoid competition in the database , to cope with this, you need to coordinate the actions of all users in all browsers, and for this you will need internal push technology, such as WebSockets (more on this below), which will Broadcast changes to everyone who runs the application.
This rather confusing statement simply means that the last values assigned to the state object (defined by one user working in its browser) become predictable. Which is actually not progress at all. Because you could use the old mutated variable to track your state, and you would know that you are dealing with the latest information every time you access it, and this will work with anyone other than React / Redux.
What for? Since React is special ... and the state of the component is controlled through a chain of events that you cannot control, and you rely on not changing the state directly . This was remarkably handled in terms of PR, as React ads turned the flaw into a sexy fashion. In addition to fashion, I would prefer to see the immutability of what it is, that is, a tool to eliminate the gap when the framework you choose does not handle the state intuitively.
3) Racing conditions are categorically bad.
Well, they can be if you use React. But they are rare if you take other frames.
In addition, you usually have much bigger problems that you have to deal with ... Problems such as addiction hell. Like a bloated codebase. How your CSS is not loading. Like a slow build process or binding to a monolithic backend, which makes iteration almost impossible. Like inexperienced developers who do not understand what is happening and make a mess.
You know. Reality. But hey, who cares?
4) Immutability uses reference types to reduce the performance impact of tracking each state change.
Because, seriously, if you are going to copy things every time your state changes, you better make sure that you are smart about it.
5) Consistency allows you to undo things .
Because ... this is the number one feature the project manager will ask for, right?
6) Fixed state has many interesting features in conjunction with WebSockets
And last but not least, the accumulation of state deltas leads to compelling arguments in conjunction with WebSockets, which makes it easy to use state as a stream of immutable events ...
As soon as a penny falls on this concept (a state is a stream of events, not a rough set of records representing the latest point of view), an unchanging world becomes a magical place to live. A country of wonderful events and opportunities that go beyond time itself. And if everything is done correctly, this can definitely simplify the execution of real-time applications, you just broadcast the stream of events to everyone interested so that they can create their own idea of the present and write their changes to the general stream.
But at some point you wake up and realize that all this miracle and magic do not come for free. Unlike your impatient colleagues, your stakeholders (yes, the people who pay you) care little about fashion and much about the money they pay to create a product that they can sell. And the bottom line is that it's harder to write immutable code and easier to crack, plus it makes little sense to have an immutable external interface if you do not have an internal interface to support it. When (and if!) You finally convince your stakeholders that you should publish and use events using push technology such as WebSockets, you discover how difficult it is to scale in production .
Now for some advice if you decide to accept it.
Choosing to write JavaScript using FP / Immutability is also a choice to make the codebase of your application bigger, harder and harder to manage. I would strongly advise limiting this approach to your Redux reducers ... if you don't know what you are doing. In other words: just keep it simple ™. In most cases, you will feel better. And wherever you are, I would focus on getting the benefits of immutable data passing through your (whole) application, rather than creating a purely functional interface, and that was it.

Now, if you are lucky enough that you can make choices in your work, then try to use your wisdom (or not) and do what is right, the person who pays you . You can base this on your own experience, your intuition, or what is happening around you (admittedly, if everyone uses React / Redux, then there is a good argument that it will be easier to find a resource to continue your work). Alternatively, you can try the Resume Driven Development or Hype Driven Development approaches. They may be more of your kind.
In short, I must say that for immutability it will make you fashionable with your peers, at least until the next hobby comes, and by this moment you will be happy to move on.
Now I added this as an article on my blog => Invariance in JavaScript: the opposite view . Feel free to answer there if you have strong feelings that you would like to take off your chest too;).