ROHINI, DELHI · LIVE ONLINE & CLASSROOM

Coding Bytes Team · 2 December 2026

JavaScript shallow copies and nested object identity

Trace object identity in a local JavaScript exercise. Predict shallow-copy mutations, copy the changed branch and retain clear evidence of what stayed shared.

You copy a JavaScript object, change a nested label in the copy and discover that the original changed too. The spread operator did create a different outer object. The surprise is that a property inside it still points to the same nested object. Learning to identify those two boundaries is more useful than memorising a rule that copying is either safe or unsafe.

This exercise uses invented course preferences held in memory. There are no accounts, network calls or stored customer records. Predict the results on paper, run the small example locally and explain each observation using object identity. Keep the starting data unchanged between experiments so an earlier mutation cannot quietly affect your next prediction.

Draw the objects before running the code

MDN's shallow copy explanation distinguishes copying an outer object's properties from making independent copies of nested objects. Object spread performs a shallow copy. In the example below, the two outer objects are different, while their settings properties initially refer to the same nested object. That statement is about this simple data shape, not every possible JavaScript object.

const original = {
  name: 'Practice learner',
  settings: { theme: 'light' }
};
const copy = { ...original };
console.log(copy === original);
console.log(copy.settings === original.settings);
copy.name = 'Revised learner';
copy.settings.theme = 'dark';
console.log(original.name);
console.log(original.settings.theme);

Draw one box for original and another for copy. Inside each, write the name value. Draw arrows from both settings properties to one shared settings box. Now trace the assignments. Replacing copy.name changes a property on the copied outer object. Assigning copy.settings.theme follows the shared arrow and changes the object reached through it.

Separate identity checks from value checks

The first comparison prints false; the second prints true. The final lines print Practice learner and dark. Read those observations together. The unchanged name does not prove complete independence, and the changed theme does not prove that both variables refer to the same outer object. Both shortcuts hide the boundary you need to understand.

ObservationQuestion it answers
copy === originalAre these the same outer object?
copy.settings === original.settingsDo these properties reach the same settings object?
original.nameWas this outer property replaced?
original.settings.themeWas the shared nested object changed?

Log primitive values and explicit comparisons as above. A debugging display of an entire object may be less clear about when you inspected it. Your explanation should identify the assignment and the object it reached, rather than relying only on how a collapsed console preview happens to look.

Copy the branch that you intend to change

Start again with fresh data. This time, create a new settings object as part of preparing the changed value. The important difference is the extra copy at the settings boundary. It gives this particular branch an independent object; it is not a recursive copying operation for arbitrary deeper data.

const source = {
  name: 'Practice learner',
  settings: { theme: 'light' }
};
const revised = {
  ...source,
  settings: { ...source.settings, theme: 'dark' }
};
console.log(revised.settings === source.settings);
console.log(source.settings.theme);
console.log(revised.settings.theme);

The results are false, light and dark. Add a second level beneath settings in a separate experiment and draw its arrows before changing it. Copying settings does not automatically copy an object stored inside settings. The useful habit is tracing every branch you plan to mutate, not counting how many spread operators appear in the file.

Choose a change policy for the exercise

Suppose a practice interface keeps a saved preference object and a temporary editing value. Write down whether editing should affect the saved value immediately or only after a deliberate save. That requirement determines what independence you need. An accidental shared mutation is especially confusing when a Cancel button appears to discard work that already changed the saved object.

For a local experiment, implement only the data transformation first. Prepare an edited value, compare the relevant identities and confirm that the saved value still has its original theme. Then discard the edited value and check the saved value again. You can reason about that contract without introducing a framework, a server or real user settings.

Do not replace the problem with a universal copying recipe. Different values have different behaviour, and a copy strategy must fit the data and purpose. If your actual application includes dates, maps, functions or custom instances, investigate those requirements separately. This exercise deliberately stays with plain objects containing strings.

Submit a small evidence record

Keep the starting code, your predicted outputs, actual outputs and the object diagram. Add one sentence for each changed value: which assignment ran, which object it reached and why the other reference did or did not observe it. If a prediction failed, retain it beside the corrected explanation; that is useful debugging evidence.

For an additional challenge, pass the settings object to a function and change its theme inside that function. Predict whether moving the assignment into a function changes the shared identity. Compare your answer with the earlier direct assignment before running the experiment. The language mechanism stays the focus, rather than a larger interface hiding the cause.

Common questions

Does spread always produce a different outer object here?

Yes, the object literal with spread creates a new outer object in these examples. Its nested property can still refer to an existing object.

Must every nested object be copied?

Copy according to the changes and independence your program requires. An untouched branch may be shared deliberately; document that choice.

Can this explain every state-management bug?

No. It isolates object identity and mutation. Timing, rendering and persistence introduce separate questions that need their own checks.

Continue with response ownership in a frontend exercise, or ask about a free demo class for practical programming study.

Related study: Full stack with React and Node.js · Web development with Python.

Admissions

Request a call back

Tell us which course you are interested in and the counselling desk will call you about fees, batches and a free demo class.

18008913982 (toll free) · +91 9899-355-335
[email protected]

Message us on WhatsApp

Open the enquiry form in a separate window ↗

Your enquiry goes to Coding Bytes through ViaCRM so the counselling team can respond. Please share only the details needed for your question, not passwords, payment details or sensitive documents. Privacy & enquiries · Before enrolling

Call WhatsApp Enquire