Sunday, December 16, 2018

JavaScript Mutable and Immutable Objects



In JavaScript Primitives are immutable, which means that the value is passed instead of reference.
For Objects the reference is passed instead of value. Example as below,

//Primitives are immutable
var a = 10;
var b = a;

console.log("a: "+a);
console.log("b: "+b);

a =20;

console.log("a: "+a);
console.log("b: "+b);

//Mutable
var c = {name: "Zack"};

var d = c;
console.log("c: "+c.name);
console.log("d: "+d.name);
d.name = "Arnold";
console.log("c: "+c.name);
console.log("d: "+d.name);

Ensure whether you are passing value or reference during development. This is one of the basic concepts in JavaScript. 

3 comments:

  1. Hello, an amazing Information dude. Thanks for sharing this nice information with us. Rapid Covid Test

    ReplyDelete
  2. What a nice Blog, thanks for sharing such important info about Industrial gases supplier .Keep learning Keep loving.

    ReplyDelete
  3. Magnificent work.

    Thank you for sharing such a detailed information.

    we at Glocalview infotech Provides All kind of web development & Mobile app development services

    ReplyDelete