Navigate back to the homepage

How to assign object without reference in javascript?

Infinitbility
JavaScript
December 20th, 2021 · 1 min read
How to assign object without reference in javascript?

Hello Friends 👋,

Welcome to Infinitbility.

When we assign one object data in another new variable still they both are connected to each other using the javascript reference concept or if change anything on the object variable you will same changes in another object.

For example, when we copy firstObject to secondObject and change secondObject it’s will change firstObject.

1let firstObject = { a: 1, b: 2};
2
3// copy objects with references
4let secondObject = firstObject;
5console.log("firstObject", firstObject);
6console.log("secondObject", secondObject);
7
8// assigning new element in the second object
9secondObject.c = 3;
10
11// affecting both objects
12console.log("firstObject", firstObject);
13console.log("secondObject", secondObject);
Javascript Object Referance
Javascript Object copy with Referance

Now, we know problem, let move to solution.

javascript provide multipe ways to create javascript object with data and Object.create() is one of them it will help you to copy object without any referance.

For test let use Object.create() method on our example.

1let firstObject = { a: 1, b: 2};
2
3// copy objects with references
4let secondObject = Object.create(firstObject);
5console.log("firstObject", firstObject);
6console.log("secondObject", secondObject);
7
8// assigning new element in the second object
9secondObject.c = 3;
10
11// affecting both objects
12console.log("firstObject", firstObject);
13console.log("secondObject", secondObject);
Javascript Object without Referance
Javascript Object copy without Referance

Conlustion, When we want copy any object without referance then we have to use object create method, it will copy data in new object.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to enable or disable screenshot in react native?

How to enable or disable screenshot in react native?

React Native screen capture secure example

December 7th, 2021 · 1 min read
How to delete item from AsyncStorage in react native?

How to delete item from AsyncStorage in react native?

React native asyncstorage remove item example

December 6th, 2021 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]