← All tasksJavaScript · Middle
deepEqual: deep comparison
Write a function deepEqual(a, b) that compares values by content: primitives, arrays and plain objects at any depth. Key order in objects doesn't matter.
An array isn't equal to an object with the same keys, { a: undefined } isn't equal to { b: undefined }, and NaN equals NaN.
Ctrl+Enter runs the tests. Tab indents; to leave the editor with the keyboard, press Esc and then Tab. Your code is saved in this browser.
Tests (8)
expect(deepEqual({ a: 1, b: [1, 2] }, { b: [1, 2], a: 1 })).toBe(true);
expect(deepEqual({ a: { b: { c: 1 } } }, { a: { b: { c: 2 } } })).toBe(false);
expect(deepEqual([1, [2, [3]]], [1, [2, [3]]])).toBe(true);
expect(deepEqual([1, 2], { 0: 1, 1: 2 })).toBe(false);
expect(deepEqual({ a: undefined }, { b: undefined })).toBe(false);
expect(deepEqual(NaN, NaN)).toBe(true);
expect(deepEqual(null, {})).toBe(false); expect(deepEqual(null, null)).toBe(true);
expect(deepEqual(1, '1')).toBe(false);