Javascript Mutables and Immutables
Understand mutable and immutable in javascript.
Mutable and immutable in Javascript
There are two data types in javascipt , Primitive(Value type) and Non-primitive(Reference Type)
Primitive | Non-Primitive |
---|---|
Numbers | Array |
Boolean | Object |
String | Function |
Null | Date |
Undefined |
Mutable are the one which can be modified after it has been created. Immutable are those which cannot be modified after they are created.
In javascript, only ==Arrays== and ==Objects== are mutable.
It is very important to understand this concept in javascript, because there are some built-in methods in javascript that mutate the values of original data.
Knowing this before hand will help you understand the logic and write better code so as to avoid any kind of unexpected behaviour while working on a real world application.