Deep Equality

ObjectsRecursionComparison

Write deepEqual(a, b) returning whether the two values are structurally equal.

Two objects are equal when they have the same keys and every corresponding value is deeply equal; key order does not matter. Two arrays are equal when they have the same length and equal values in the same order. Primitives compare with ===, except that NaN equals NaN.

Examples

Example 1
Input
deepEqual(
  { id: 7, tags: ['a', 'b'] },
  { id: 7, tags: ['b', 'a'] }
);
Output
false
Explanation
The keys match and so does id, but the two tags arrays hold the same items in a different order, and array order counts.

Constraints

  • Values are plain objects, arrays or primitives, including undefined and NaN
  • No value contains a reference to itself

Notes

  • An array is never equal to an object, even one with matching numeric keys.
  • `typeof null` is "object", so it needs its own guard.

Hints

Read the full write-up for Deep Equality
</>JavaScript
Loading editor…
Test Result
Run your code, or Submit to test it