Technical skill practice
Create a Nested Object in JavaScript: Updates and Shared References
A nested object is an object stored as a property value. Create the intermediate object before assigning one of its fields. Assigning the same child object to another property shares that reference rather than copying it.
The task
Create a workspace configuration with export options, attach the options to another record and test an assignment through a missing intermediate path.
Runnable Node 22 example
const assert = require('node:assert/strict');
const config = {workspace: {export: {format: 'pdf'}}};
config.workspace.export.format = 'text';
const mirror = {options: config.workspace.export};
mirror.options.format = 'json';
assert.equal(config.workspace.export.format, 'json');
assert.throws(() => { config.delivery.email = true; }, TypeError);
config.delivery = {email: true};
assert.equal(config.delivery.email, true);
console.log(JSON.stringify(config));
Expected output and explanation
The first update reaches an existing nested export object. mirror then points to that same object, so its later update appears through config too. The relationship is intentional sharing, not a second independent configuration.
The delivery assignment fails before the intermediate object exists. Creating delivery with the intended shape makes the next check succeed. Optional chaining is useful for some reads, but it does not provide a general assignment mechanism that invents missing intermediate structures.
Output from the example
{"workspace":{"export":{"format":"json"}},"delivery":{"email":true}}
Pitfall to check
A shallow outer copy would still share existing child objects. If an update must be independent, duplicate the required nested structure deliberately and test the resulting references. Do not accept arbitrary property paths from external input without defining permitted fields.
Questions, answered
Does const make every nested field immutable?
No. const prevents rebinding config itself; it does not recursively freeze the objects it references.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.