JavaScript, inside out · Chapter 48 of 51

Deep clone and deep equal

By the end of this chapter you can walk any nested value yourself: copy it without sharing anything, compare two values by what they hold, and flatten one into dotted keys. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The interview
  • 5 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

Round three. Dev tells a story instead of asking a question. "A company I advise copied a customer's cart with spread, applied a coupon to the copy, and the original cart changed too. Two hundred customers were charged the discounted price twice. Tell me why, then write me a copy that cannot do that." After that he wants a deep equal for the tests, and a flatten for Ravi's analytics sheet, which only takes flat rows.

Spread (chapter 16) is not the answer: it copies one level, and everything below that level stays shared. And === cannot compare two copies, because it only asks whether they are the same object.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1 · interview question

    Interview: deepClone, nested objects and arrays

    Write deepClone(value). It returns a deep copy: every array becomes a new array and every plain object a new object, at every level, so changing the copy at any depth never changes the original. Arrays stay arrays.

    Keys whose value is undefined, NaN or a function are kept (a function is the same function, not a copy). Primitives, null and undefined come back as they are.

    Write it yourself: structuredClone is not allowed (the check fails if you call it), and a JSON round trip loses undefined, NaN and functions.

  2. Task 2 · interview question

    Interview: deepClone, Dates, Maps and Sets

    Make deepClone copy Dates, Maps and Sets as real copies of the same type, wherever they appear (on their own, inside an object, inside an array).

    A Date becomes a new Date with the same time. A Map becomes a new Map with the same keys in the same order, each value deep cloned. A Set becomes a new Set with its members in the same order, each member deep cloned. Changing the copy (setting the Date's year, adding to the Map or Set, changing an object inside them) leaves the original alone.

  3. Task 3 · interview question

    Interview: deepClone, circular and shared references

    Make deepClone work on values that point back at themselves, and keep shared references shared.

    If an object's self key points at the object, the copy's self points at the copy. The same holds for cycles through arrays, nested objects, Map values and Set members. If two fields of the original point at one object, the same two fields of the copy point at one copy (a new object, not the original).

  4. Task 4 · interview question

    Interview: deepEqual, compare two values by value

    Write deepEqual(a, b). Return true when a and b are equal by value, otherwise false.

    Primitives are equal when they are triple equal, with one exception: two NaNs are equal. null equals only null, so null and undefined are not equal, and 40 and '40' are not equal.

    Two Dates are equal when they hold the same time; a Date never equals a non-Date. Arrays are equal to arrays with equal elements in the same order and the same length; an array never equals a plain object, even one with keys 0 and 1. Objects are equal when they have the same own keys, in any order, and deeply equal values for every key. So objects with different keys are not equal, even when both have the same number of keys whose values are undefined.

  5. Task 5 · interview question

    Interview: flatten, nested keys to dotted keys

    Write flatten(obj). Return a new object with one level: every nested value gets a key that is the path to it, its keys joined with dots. Array indexes are keys too, so the first item of tags becomes tags.0.

    These are values, kept as they are under their own path, not walked into: null, undefined, Dates (the same Date object), and empty objects and empty arrays. Top-level keys with plain values stay as they are, and an empty object gives an empty object.

    Do not change the input, and start every call from a fresh result.

What you will learn

  • Why the quick fixes fail: spread, the JSON round trip and ===.
  • Telling types apart: null, arrays, Dates, Maps and Sets all say 'object'.
  • Walking a structure with recursion: with a memory of what you have already visited, so a loop cannot trap you.
  • Carrying a path down the recursion: so each value knows where it was found.

More in The interview

Build "Deep clone and deep equal" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.