Check if two values are deeply equal
Two objects with the same contents are not equal with the === operator, which compares references. To compare by value, use a deep equality check. This example shows the two built-in options.
Reference comparison fails even when the contents match.
const first = { a: 1 };
const second = { a: 1 };
console.log(first === second); // falseThe standard library exports equal, a non-throwing deep comparison.
import { equal } from "jsr:@std/assert";
console.log(equal({ a: [1, 2] }, { a: [1, 2] })); // true
console.log(equal(new Map([["a", 1]]), new Map([["a", 1]]))); // true
console.log(equal({ a: 1 }, { a: 1, b: undefined })); // falseNode.js compatibility offers isDeepStrictEqual with the same job.
import { isDeepStrictEqual } from "node:util";
console.log(isDeepStrictEqual({ a: [1, 2] }, { a: [1, 2] })); // trueStrict means types matter. A string is never equal to a number.
console.log(isDeepStrictEqual({ a: 1 }, { a: "1" })); // falseIn tests, prefer an asserting variant, which throws with a readable diff of the two values on failure.
import { assertEquals } from "jsr:@std/assert";
assertEquals({ a: [1, 2] }, { a: [1, 2] });
console.log("assertEquals passed"); // assertEquals passedThe Node.js equivalent assertion also works in Deno.
import { deepStrictEqual } from "node:assert/strict";
deepStrictEqual({ a: [1, 2] }, { a: [1, 2] });
console.log("deepStrictEqual passed"); // deepStrictEqual passedRun this example locally using the Deno CLI:
deno run https://docs.deno.com/examples/scripts/deep_equal.ts