Understanding concepts of immutables and mutables in javascript

  Javascript strings are immutable.But javascript objects and arrays are mutable.A mutable object is an object whose content can be modified after it is created. An immutable object is an object whose content cannot be modified after it is created. Consider an the following example

Now,from the above we realize that arrays and objects in javascript are mutable,ie they can be mutated. Functions like slice returns a new reference and does not modify the original array. Example

filter(),map(),reduce()  are the other functions that does not modify original array. Javascript es2016 brings a new operator called spread operator.Spread operator helps copy a array to a new array with new reference.

Usig spread operator(…a) along operations like slice,filter ….. helps operate with javascript arrays in an immutable way. Spread operator on object

Why Immutability? We look at  immutability from an angular point of view to get a better understanding of the needs if immutability and the reason why it increases performance.Consider we have a parent component and child component.From the parent component an object is shared to the child component.Lets say the following is the object is the one that is shared

Consider a click event function…

Share this page in social media platforms