var a =10; var b =10;

var c = 20; var v = c; //reference is assigned instead, it will create a new copy(memory reference).

In JavaScript, Objects are often called reference types, unlike primitive types. Instead of storing the value directly inside those variables. Objects store a reference to that value somewhere else in the memory, so you storing a reference to the objects instead; that's why they are called reference types.

Let obj={name:"shivaji"} Let obj2={name:"shivaji"}

When you create both of these objects, these values are assigned some memory, both of these objects are stored at separate locations, and then both of these variables, obj and obj2, store the respective addresses of their values. When you compare obj with obj2

Pass by value or call by value: primitive values. A new memory reference will be created.

Pass by reference: non primitive like objects and arrays. Assigned memory reference. If you update the properties, the data will be overwritten, so the original object will be affected. A copy of the reference to that object is copied to the second object. Even though these are two different variables, both of them are storing the same reference and the same value, which is a reference to a common value in memory.

What if you re-assign a new object Obj={ Name: "Shivaji" } This object will be assigned some value in memory, and then the reference to the old object will be updated to the New memory location instead. So now, obj refers to a completely different object in memory. Call by sharing, pass by sharing or call by object sharing.

Deep copy :

  1. Spread operator.
  2. Object.assign({},obj)
  3. Json.parse(Json.stringfy(obj))

There are some scenarios where we can't use JSON.parse and stringify because it won't work.

Some scenarios: Date involved.

If you use JSON.parse and stringify on those objects that contain a date, then it will convert into a string, rather than keeping the data object as it is after the deep copy. Const data={ Date: new Date() }