What is Immutability
Immutability is a design pattern where something can't be modified after being instantiated. If we want to change the value of that thing, we must recreate it with the new value instead. Some JavaScript types are immutable and some are mutable, meaning their value can change without having to recreate it. Let's explain this difference with some examples:
let movie = {
name: 'Star Wars',
episode: 7
};
let myEp = movie.episode;
movie.episode = 8;
console.log(myEp); // outputs 7
As you can see in this case, although we changed the value of
movie.episode
, the value of myEp
didn't change. That's because movie.episode
's type, number
, is immutable.let movie1 = {
name: 'Star Wars',
episode: 7
};