When I first dived into JavaScript and programming; I never really thought about immutable data. I would say animal is panda, then animal is lion.
I was free to do whatever I wanted with my data! But… things changed… I grew up. People started telling me: “you should always use const if you can”. So I obediently did. But I didn’t really understand why.
Because sometimes code changes things you don’t want to be changed. That’s a very lame answer I know, let me show you with an example.
Let’s say we have an e-commerce site.
Let’s say we got our address-validator by installing an npm
package.
Everything works as expected but one day a new version is released a new line of code was introduced to the package which looks like this:
Now the variable userAddress
will always be equal to the value of address! You can see how this is a problem.
This specific issue can be solved by using immutability. But it can also be solved with proper scoping. Try to figure out how!
Of course, malicious code is an edge case. But there are many ways in which immutable data can help you write better code. For example, a very common mistake is to accidentally mutate the property of an object.
This type of mistake can occur very often.
The most intuitive immutability tool is to use const
.
const
is great. However, it sometimes only gives the illusion of immutability!
There are different ways to solve this problem and introducing immutability is one of them.
First we can use the Object.freeze
method.
One issue with Object.freeze
is that you don’t affect sub-properties. To reach all sub-properties you can write something like:
Another solution is to use proxies if you need flexibility.
In many sites, you will see that you can modify property descriptors to create immutable properties. And that’s true, but you have to make sure that configurable
and writeable
are set to false.
But the data is still mutable, it is just more difficult to mutate it:
Arr!
If we set writable and configurable to false we get:
Arrays have the same problem as Objects.
Well you can use the same tools we used above.
There are other tricks to keep your data safe. I highly recommend checking out our article about Proxy Traps to find out other ways to make your data immutable. We only scratched the surface here.
In this post, we explored options to introduce immutability without changing the structure and style of our code. In future posts, we will explore libraries such as Immutable.js, Immer or Ramda to right immutable code.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hey, the example showing the value of address to ‘123 My Street, Bring Me Free Goods, USA’ seems incorrect.
Take a look at this snippet.
Same result if the user.address is a object instead of a string.