Implement shallowClone() in JavaScript

easy
By FrontendAtlas Editorial · Updated Oct 21, 2026
Implement shallowClone(value) in JavaScript. Your function should accept an object or array and return a new top-level container while preserving nested object and array references. Array holes, length, custom own enumerable string properties, and own enumerable symbol properties are part of the contract. Primitives, null, and unsupported root values should throw TypeError. This challenge includes starter code, tests, examples for object and array cloning, shallow vs deep clone edge cases, and interview follow-ups around Object.assign(), spread syntax, prototypes, descriptors, and structuredClone().

Expected behavior

Your shallowClone(value) implementation should:

  • Accept only objects and arrays as root inputs.
  • Arrays return a new array reference with the same length, holes, top-level items, and custom own enumerable properties.
  • Objects return a new object reference with the same own enumerable properties.
  • Top-level primitive property and item values are copied by value inside the cloned container.
  • Nested objects and arrays remain shared references.
  • Updating a top-level property or array slot on the clone does not mutate the original container.
  • Mutating a nested object or nested array through the clone is visible through the original.
  • null and primitive root inputs throw TypeError.
  • The base solution copies own enumerable string and symbol properties.
  • Prototype chains and property descriptors are not preserved.

Arguments

  • value: Object | Array — The object or array to clone.

Returns

Object | Array — A new shallow-cloned object or array.

Solution idea

Interview answer: the base prompt is to create a new top-level container while preserving nested references. Guard null and primitive values first. For arrays, allocate new Array(value.length) and copy with Object.assign so holes and length survive while custom own enumerable string and symbol properties are included. For objects, use Object.assign({}, value). Both branches ignore inherited prototype properties. Deep cloning, structuredClone(), JSON cloning, prototypes, descriptors, Maps, Sets, Dates, and circular references are follow-ups, not part of the minimal shallowClone(value) contract.

Examples
// Array shallow clone
const original = [1, { count: 2 }];
const copy = shallowClone(original);

copy === original; // false
copy[1] === original[1]; // true

copy[0] = 99;
original[0]; // 1

copy[1].count = 3;
original[1].count; // 3

// Object shallow clone
const user = { name: 'Ada', meta: { active: true } };
const cloned = shallowClone(user);

cloned === user; // false
cloned.meta === user.meta; // true

Public interview reports

These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.

Reported in a public Tesla interview account. Tesla frontend interview questions

Reported in a public Shopee interview account. Shopee frontend interview questions