Search code examples
javascriptarraysdeepequals

Are arrays and objects with numeric properties equal in javascript?


Can someone explain to me why the following is true:

let foo = { 
    A: [ 1, 2 ] 
}
let bar = {
   "A": {
      "0": "1",
      "1": "2"
   }
}
assert.deepEqual(foo, bar);

Solution

  • As the documentation says:

    Only enumerable "own" properties are considered. The assert.deepEqual() implementation does not test the [[Prototype]] of objects or enumerable own Symbol properties. For such checks, consider using assert.deepStrictEqual() instead.

    The assert.deepStrictEqual() function does check the prototype too, and

    assert.deepStrictEqual(foo, bar);
    

    will return false.