Looking at Node.js documentation on the assert
module, I see functions like assert.equal
, assert.strictEqual
and assert.deepEqual
.
What exactly is the difference between equal, deep equal, and strict equal?
As others have stated, the documentation does say what the difference is, but maybe it is not as easy to see.
assert.equal works the same as using a double equals in JavaScript:
assert.equal(50, 50); //true
assert.equal(50, "50"); //true
assert.equal(1, true); //true
assert.equal(50, 70); //false
assert.equal({n:1}, {n:1}); //false
This means that you get value checking, and JavaScripts fancy type conversions kick in.
assert.strictEqual works just like the triple equals in JavaScript:
assert.strictEqual(50, 50); //true
assert.strictEqual(50, "50"); //false
assert.strictEqual(1, true); //false
assert.strictEqual(50, 70); //false
assert.equal({n:1}, {n:1}); //false
This means that you get strict comparison checking both value and type.
Both methods check complex objects by reference, not value. That is where deepEquals comes in. deepEquals check objects and their properties for value equality:
assert.deepEqual({n:1}, {n:1}); //true
assert.deepEqual({n:1}, {n:true}); //true
assert.deepEqual({n:1}, {n:"1"}); //true
deepEquals does a == comparison of type and value for nested properties and child objects of complex objects.
Then you have deepStrictEqual which is just like deepEqual, except that it adds the strict checking of type and value:
assert.deepStrictEqual({n:1}, {n:1}); //true
assert.deepStrictEqual({n:1}, {n:true}); //false
assert.deepStrictEqual({n:1}, {n:"1"}); //false
So equal and strictEqual test equality of value, and strict adds type, deepEqual and deepStrictEqual check for two objects contain the same properties and values.
W3C Schools have some nice, simple documentation on these topics: assert.equal assert.strictEqual assert.deepEqual assert.deepStrictEqual
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With