JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – Did the object assign method really pass reference

JavaScript Quiz – Did the object assign method really pass reference

Object.assign() Method –

Object.assign() is an object assign method which copies all key-value pairs from the one or more source object to the target object and returns a new object.

It’s syntax like – Object.assign(target, ...sources)
To know more about JavaScript Object.assign() Method

Question –

First, we declare two variable objOne & objTwo using let keyword, then assign them a different-different object value.

Now, create a new variable newObj using let. And then we use Object.assign() method to assign object to newObj by combining two previously created object (objOne, objTwo).

Fun part begin, we change the value of key in newObj object.

first name key value change to “NodeJS” which is previously “JS”

last name key value change to “ReactJS” which is previously “Startup”.

Here, you need to find out that after done changes in newObj, does it also affect the objOne & objTwo.

Answer – “NodeJS”, “Startup”

Click To Run code

Explaination –

Why the answer is “NodeJS”, “Startup”?

Does the objOne object is the only one to pass their reference to newObj object, not the objTwo object? Let’s find out…

As we know in the object assign method there are two-term target and sources. So, all source object is merged to the target object.

And then that, the target object is returned and assigned to newObj. So, only objOne object is only the one which pass their reference to newObj object.

One more thing, first all sources key-value pair is merge to target means objTwo key-value pair assign to objOne. Then it goes to newObj object.

At this point, newObj object and objOne are same. If anything happens to either of them, changes will reflect to both of them.

But, the objTwo object does not have any impact of changes as there is no reference pass to the newObj object.

That’s why, when newObj[“first”] value change to “NodeJS”. It also changes the objOne[“first”] value also.

But when newObj[“last”] value change to “ReactJS”. It also change the objOne[“last”] value but not the objTwo[“last”].

So, i hope you understand the whole scenario.

Task For You –

Here, is a task for you. Let me know in the comment section. How we remove the reference from objOne object also like objTwo object.

So, that if any change occur to newObj object. It not affect the objOne object.

But you have to do this using object assign method only.

Check out our other javascript quiz – 

  1. FIND OUT THE SIZE OF A JAVASCRIPT MAP OBJECT
  2. DOES NESTED ARRAY REFERENCE REMOVED BY SPREAD SYNTAX
  3. FIND OUT THE OUTPUT OF A REVERSE STRING

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
10 Shares
Share via
Copy link