What is immutable in JavaScript
Understanding Immutability
When we start talking about JavaScript, one of the key concepts that often comes up is immutability. Simply put, immutability is a term used to describe something that cannot be changed. You can think of it as a frozen pizza. Once you've cooked it, you can't un-cook it or change its original form. In the context of JavaScript, an immutable object is an object whose state cannot be modified after it is created. But don't fret, we'll get down to the nuts and bolts of it all in a moment.
The Basics of Immutability
In JavaScript, there are two types of data: primitive and non-primitive. Primitive types (like numbers, strings, and booleans) are immutable. This means that once a value is created, it can't be changed. Let's look at an example:
let a = 'hello';
let b = a;
b = 'world';
console.log(a); // 'hello'
console.log(b); // 'world'
Although we reassigned b
, the value of a
did not change. This is because when we did let b = a
, JavaScript created a copy of a
's value for b
. So changing b
didn't affect a
.
Now, non-primitive types (like objects and arrays) are a different story. They are mutable, which means we can change their properties or elements.
let obj1 = { name: 'Alice' };
let obj2 = obj1;
obj2.name = 'Bob';
console.log(obj1.name); // 'Bob'
console.log(obj2.name); // 'Bob'
This time, both obj1
and obj2
changed. This is because obj1
and obj2
are pointing to the same object in memory. When we change obj2
, we are also changing obj1
.
Why Do We Care About Immutability?
Now that we understand what immutability is, let's discuss why it's important. Immutability helps us write safer and cleaner code.
Imagine you're at a museum. The museum rules state that you can't touch any exhibits. The exhibits are immutable. Why? Because if people could touch and change the exhibits, things would quickly get chaotic.
The same principle applies to programming. By keeping our data immutable, we can avoid unexpected changes and bugs in our code.
Making Objects and Arrays Immutable
While objects and arrays in JavaScript are mutable by default, there are ways to make them immutable. One way is by using the Object.freeze()
method.
let obj = { name: 'Alice' };
Object.freeze(obj);
obj.name = 'Bob';
console.log(obj.name); // 'Alice'
As you can see, even though we tried to change obj
, its value stayed the same. This is because Object.freeze()
made obj
immutable.
Immutability and Functional Programming
Immutability is a core concept in functional programming, a popular paradigm in JavaScript. Functional programming treats data as immutable and uses pure functions (functions that don't change their input and always give the same output for the same input) to change data.
This way of programming reduces side effects (unintended changes to data) and makes code easier to understand and debug. Here's an example of how you can use the map()
function to create a new array with incremented values, rather than changing the original array:
let arr = [1, 2, 3];
let newArr = arr.map(x => x + 1);
console.log(arr); // [1, 2, 3]
console.log(newArr); // [2, 3, 4]
In Conclusion
Immutability, the frozen pizza of JavaScript, is a mighty concept that can save you from many headaches in your coding journey. It's a beacon of stability in a sea of change, a steadfast rule that once something is created, it shall not be changed.
By understanding and using immutability, you're not only levelling up your JavaScript knowledge, but also joining the ranks of functional programmers who swear by the power of this principle. So, next time you're about to mutate an object, think twice. There's a good chance that you're better off with immutability!