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?
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?
Share Improve this question edited Sep 26, 2022 at 19:27 trincot 350k36 gold badges271 silver badges322 bronze badges asked May 25, 2013 at 2:29 Derek ChiangDerek Chiang 3,4106 gold badges29 silver badges37 bronze badges 8- 4 The documentation says it all. – Joseph Commented May 25, 2013 at 2:31
- 3 @JosephtheDreamer to be fair, the page does not define "deep equal." – Matt Ball Commented May 25, 2013 at 2:34
- 1 Just so you know, deep in the OO context means somethin like "the object and all its properties, all those properties' properties and so on." Aw, you get the picture. – acdcjunior Commented May 25, 2013 at 2:41
- looked at that page in 2017 and got here:) – Suhail Mumtaz Awan Commented Mar 28, 2017 at 13:45
- There is a good explanation and example about Shallow and Deep comparison here: blog.webnersolutions.com/… – Yusril Maulidan Raji Commented Jan 9, 2019 at 8:58
1 Answer
Reset to default 14As 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