Immutability is used in JavaScript and especially React because it's a core computer science concept with real-world benefits, not because it's "sexy" or a fad.
Arguably the largest benefit in React-land is being able to pass around objects and do equality checks via reference. If I mutate an object and pass it as a prop down the tree, the consumer components won't know that it's been updated because React does shallow comparison by default (===). Meaning that wherever you have props in your code where you mutate an object, you have to write a function to determine equality, which leads to your code becoming bloated and unreadable, not to mention the massive loss in performance.
Mutability also harms our ability to test for expected outputs from given inputs. Mutations can cause side-effects that cause other mutations which can be difficult to see or predict. This means code cannot be accurately tested, and it becomes much more difficult to find bugs and near impossible to get a macro-level view of your application logic. In this sense, mutability can make your application /more/ complex and difficult to maintain and extend.
Edit: Please read the other responses in that SO thread. They contain correct information while most of yours is misinformation/speculation with an authoritative tone.
I am saying this because I actually want someone to come up with a convincing argument that will disuade me that immutability in JavaScript is actually something more than a passing fashion driven by React's popularity and its poorly designed state management.
When you speak of side-effects, I am hoping that you are talking about something real that you have experienced in a context other than React, because I have been programming JavaScript for over 15 years (vanilla.js, prototype, jquery, mootools, d3, ext.js, backbone, knockout, angular, vue, as well as plenty of back-end stuff in node.js) and I can tell you that 'mutations' have never been a problem until React came along.
8 comments
[ 1.9 ms ] story [ 28.6 ms ] threadArguably the largest benefit in React-land is being able to pass around objects and do equality checks via reference. If I mutate an object and pass it as a prop down the tree, the consumer components won't know that it's been updated because React does shallow comparison by default (===). Meaning that wherever you have props in your code where you mutate an object, you have to write a function to determine equality, which leads to your code becoming bloated and unreadable, not to mention the massive loss in performance.
Mutability also harms our ability to test for expected outputs from given inputs. Mutations can cause side-effects that cause other mutations which can be difficult to see or predict. This means code cannot be accurately tested, and it becomes much more difficult to find bugs and near impossible to get a macro-level view of your application logic. In this sense, mutability can make your application /more/ complex and difficult to maintain and extend.
Edit: Please read the other responses in that SO thread. They contain correct information while most of yours is misinformation/speculation with an authoritative tone.
When you speak of side-effects, I am hoping that you are talking about something real that you have experienced in a context other than React, because I have been programming JavaScript for over 15 years (vanilla.js, prototype, jquery, mootools, d3, ext.js, backbone, knockout, angular, vue, as well as plenty of back-end stuff in node.js) and I can tell you that 'mutations' have never been a problem until React came along.