Explain how function works

function deepEqual(a, b) {
  if (a === b) return true;
  
  if (a == null || typeof a != "object" ||
      b == null || typeof b != "object")
    return false;
  
  var propsInA = 0, propsInB = 0;

  for (var prop in a)
    propsInA += 1;

  for (var prop in b) {
    propsInB += 1;
    if (!(prop in a) || !deepEqual(a[prop], b[prop]))
      return false;
  }

  return propsInA == propsInB;
}
var obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));

Please explain to me this part of code:
 (!(prop in a) || !deepEqual(a[prop], b[prop]))
      return false;
  }
  return propsInA == propsInB;
}

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

1 Like

It is a function to check if two variables are exactly the same (if a “deep-equals” b).

You can read the “!” as “not”, or as opposite, so:

(!(prop in a) -> If there’s no prop in a. Or, if (prop in a) looks to evaluate a truthy, !(prop in a) looks to evaluate the opposite, to negate the condition.

|| -> Or

!deepEqual(a[prop], b[prop])) -> I find this one more complicated, I guess it evaluates if that function does not have those parameters?

Then return false;

Take it with a grain of salt, I’m a newbie.

1 Like