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({ x: [1, 2] }, { x: [1, 2] });
Output
true
Explanation
Different objects, identical structure.

Constraints

  • Values contain only objects, arrays, numbers, strings, booleans and null

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