Immutable Primitive Values:
In JavaScript there are 5 primitive types: undefined , null , boolean , string and number.
In javascript the Primitive values are data that are stored on the stack . Primitive value is stored directly in the location that the variable accesses.
Primitive values are immutable because there is no way to change a primitive value.
If you want to access str variable it always give original value because primitive values are store by value.
Mutable Object:In JavaScript there are 5 primitive types: undefined , null , boolean , string and number.
In javascript the Primitive values are data that are stored on the stack . Primitive value is stored directly in the location that the variable accesses.
Primitive values are immutable because there is no way to change a primitive value.
Example:
var str="javascript";
str.concat("is a scriping language");//Just concatinating the string to existing string
console.log(str);//output will be original value only:
str.concat("is a scriping language");//Just concatinating the string to existing string
console.log(str);//output will be original value only:
If you want to access str variable it always give original value because primitive values are store by value.
In Javascript Objects are mutable means that can be changed. Once object is created later will add property's, delete the property's and change the property values.
Example:
var subject={
name:"Javascript"
}
console.log(subject.name)-----Javascript
subject.name="Object Oriented Javascript";
console.log(subject.name)------Object Oriented Javascript
delete subject.name-----true
subject.hasOwnProperty('name');----false
Here the name property deleted so that by default in javascript objects are mutable( can be changed)
Immutable Object:
An immutable object is an object whose state cannot be modified after it is created.
Bydefault in javascript every object is mutable if you want add object as immutable have to use ob.freeze(object) concept.
The object being frozen is immutable.Once object is immutable it prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their enumerability, configurability, or writability, from being changed, it also prevents the prototype from being changed.
'use strict';
var course={
name:"Javascript",
getCourseName:function(){
}
}
Object.freeze(course);
course.type="scripting";
once execute the above line will get below error
Uncaught TypeError: Cannot add property type, object is not extensible
delete course.name;
Cannot delete property 'name' of #<Object>