Scriptdrill

Level 4: Data shaping Exercise 34 of 45 Pro

Deep equality

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

Return `true` when `a` and `b` hold the same data. Primitives compare with `===`, except that `NaN` equals `NaN`. Arrays are equal when they have the same length and equal items in order. Plain objects are equal when they have the same keys (in any order) with equal values; a key set to `undefined` is not the same as a missing key. Dates are equal when they hold the same time. An array never equals a plain object.

What this exercise teaches

Each type gets its own rule before the generic object comparison: Dates by getTime(), arrays by length and position, and objects by key count plus a check that every key exists on the other side. Recursion handles any depth.

A hint if you get stuck

Handle the simple cases first (`===`, `NaN`, Dates, non-objects and `null`), then arrays, then objects, calling `deepEqual` again for the items.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Data shaping