Technical skill practice
Copy an Array in JavaScript: Test Shallow and Deeper Independence
A copied array can be a new container while still sharing its records. Test both identities when your requirement is an independently editable task list. A deeper copy must support the data types you actually store.
The task
Create a shallow task-array copy and a structuredClone of the same original, then update their nested completion flags separately. The fixture contains only ordinary arrays, objects, strings and Booleans.
Runnable Node 22 example
const assert = require('node:assert/strict');
const tasks = [{name: 'audit', status: {done: false}}];
const shallow = tasks.slice();
const independent = structuredClone(tasks);
assert.notEqual(shallow, tasks);
assert.equal(shallow[0], tasks[0]);
assert.notEqual(independent[0].status, tasks[0].status);
shallow[0].status.done = true;
assert.equal(tasks[0].status.done, true);
assert.equal(independent[0].status.done, false);
independent.push({name: 'file', status: {done: false}});
assert.equal(tasks.length, 1);
console.log(`originalDone=${tasks[0].status.done}; clonedDone=${independent[0].status.done}; clonedSize=${independent.length}`);
Expected output and explanation
The shallow copy passes the container-identity check but shares the task record, so its nested edit reaches the original. The separately cloned status remains false. Adding a task to the independent array also leaves the original membership unchanged.
The assertions address two different changes: editing a shared child and adding an outer element. Testing only array length would miss the nested-sharing problem. structuredClone is available in this Node22 example, but its supported-value contract is not “every possible JavaScript object.”
Output from the example
originalDone=true; clonedDone=false; clonedSize=2
Pitfall to check
Functions and some other values cannot be cloned through structuredClone. Do not generalize the fixture to objects with methods, special identity requirements or external resources without checking their semantics. JSON serialization is not an equivalent universal substitute.
Questions, answered
Is [...tasks] a deep copy?
No. Array spread creates a new outer array with the same element references for this fixture, just as the shallow slice does.
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.