2

I have an object "a" and I assign it to another variable named "b". Now I delete some property values from "b" using delete keyword. It deletes that property from both objects "a" and "b". Why is that?

P.S: I am beginner in javascript. go easy on me.

Code: -

let a = {
  a:1,
  b:2,
  c:3
}

let b = a;
console.log(a);   // output   { a: 1, b: 2, c: 3 }
delete b.a;
console.log(a)   // Expected output { a: 1, b: 2, c: 3 }    -- Actual output { b: 2, c: 3 }

4
  • 2
    this is pass by reference, here both a and b will have same value so it is deleting from same value Commented Jul 7, 2018 at 19:27
  • 3
    Both "b" and "a" are variables pointing to the same object. Commented Jul 7, 2018 at 19:27
  • Possible duplicate of How to copy/clone a hash/object in JQuery? Commented Jul 7, 2018 at 19:28
  • you ar referring a to b. u r not copying values of a into b array. it can be b = {...a} Commented Jul 7, 2018 at 19:45

3 Answers 3

2

Your question is more related to the fact that some types are assigned by value and others by reference.

In a quick summary

Primitive types are assigned by value (Boolean, Null, Undefined, Number, String, Symbol (new in ES 6))

Non Primitive types are assigned by reference (Object, Array , Functions)


Example: Primitive types

let a = 1;
let b = a;

console.log(a); // 1
console.log(b); // 1

b = 2;

console.log(a); // 1
console.log(b); // 2

As you can see changing b will not affect a because number is assigned by value.


Example: Non Primitive types

let a = { name: 'Amr' };
let b = a;

console.log(a); // { name: 'Amr' };
console.log(b); // { name: 'Amr' };

b.name = "John";

console.log(a); // { name: 'John' };
console.log(b); // { name: 'John' };

As you can see changing b affected the value of a because it is assigned by reference, this is similar to your example, the issue is not related to delete but it is related to the fact that objects are assigned by reference so deleting key from b will affect a


Cloning:

in some situations you will need to clone your non primitive type object and not mutating the current one, you can do that using the following way:

  1. ES5 var clone = Object.assign({}, obj); OR var clone = JSON.parse(JSON.stringify(obj));

  2. ES6 var clone = { ...obj };

now updating clone will not affect obj


Finally You can read more about this topic in this link it can give you a better understanding on how this works with memory assignment illustrations

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. I have understood it. :). Can I assign non primitive types by value?
You are welcome ... probably you are asking about how to clone your object ... i have edited the answer to explain that as well :)
0

When you do :

let b = a;

You just pass reference of the object a to b. So a and b points to the same reference. Therefore changes made in any one of them will reflect in other.

Basically you have something in the memory as:

a:ref12345−−−+
             |
             |
             |    +−−−−−−−−−−−−−+                 
             +−−−>|  (object)   |                 
             |    +−−−−−−−−−−−−−+                 
             |    | prop1: "a"  |                 
             |    | prop2: "b"  |
b :ref12345−−+    | prop3: "c"  |
                  |             |
                  +−−−−−−−−−−−−−+                

Comments

0

Java script array are copied through copy by reference. So if you edit the copied array, the original array will be changed. You can use

let b = a. slice()

Or

Spread operator in ES6. let b = [... a]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.