Comparing two objects in Javascript is not trivial, you can't just use === or == to compare them, there is no out-of-the-box method. Getting the differences between them requires custom functions or third-party libraries.

Here is how you can compare two objects and extract differences between two objects that are only one level deep, meaning they consist of only {key: value} pairs.

const obj1 = {name: 'Peter', age: 36};
const obj2 = {name: 'Peter', age: 25, hairColor: 'black'};

function compareObjects(a, b) {
    for (const [key, value] of Object.entries(a)) {
        if (!b.hasOwnProperty(key)) {
            console.log(`object b doesn't have key: ${key}`);
        } else if(value !== b[key]) {
            console.log(`object b has different value for key: ${key}, object b value is: ${b[key]}`);
        }
    }
}

console.log('==Compare obj1 to obj2==');
compareObjects(obj1, obj2);

console.log('==Compare obj2 to obj1==');
compareObjects(obj2, obj1);

This will output

==Compare obj1 to obj2==
object b has different value for key: age, object b value is: 25
==Compare obj2 to obj1==
object b has different value for key: age, object b value is: 36
object b doesn't have key: hairColor

The method is simple. Iterate over obj1 and compare with obj2 values and vice versa.

Good, now you know how to compare two simple objects and get the differences between them.

How to compare two nested objects and get the differences?

Comparing nested objects and getting the differences is not something that it's worth writing from scratch. 

Third-party libraries to the rescue! 

The Javascript community is known for its large number of small libraries which are only one function. Such a library is deep-object-diff. Its sole purpose is to have an object diff function.

Let's look at how we can use this library.

import { diff, addedDiff, deletedDiff, updatedDiff, detailedDiff } from 'deep-object-diff';

const obj1 = {
    foo: {
        bar: {
            myValue: 1,
            secondValue: 1,
        }
    },
};

const obj2 = {
    foo: {
        bar: {
            myValue: 2,
        }
    },
    a: 5,
};

console.log('==Detailed diff==');
console.log(detailedDiff(obj1, obj2));
console.log('==Added diff==');
console.log(addedDiff(obj1, obj2));
console.log('==Update diff==');
console.log(updatedDiff(obj1, obj2));
console.log('==Deleted diff==');
console.log(deletedDiff(obj1, obj2));

which outputs

==Detailed diff==
{
  added: [Object: null prototype] { a: 5 },
  deleted: [Object: null prototype] {
    foo: [Object: null prototype] { bar: [Object: null prototype] }
  },
  updated: [Object: null prototype] {
    foo: [Object: null prototype] { bar: [Object: null prototype] }
  }
}
==Added diff==
[Object: null prototype] { a: 5 }
==Update diff==
[Object: null prototype] {
  foo: [Object: null prototype] {
    bar: [Object: null prototype] { myValue: 2 }
  }
}
==Deleted diff==
[Object: null prototype] {
  foo: [Object: null prototype] {
    bar: [Object: null prototype] { secondValue: undefined }
  }
}

What if I only need to compare objects?

You may only need to compare two objects without getting the differences between them. If that is the case, you can again take advantage of an existing third-party library like Lodash also known as just _

Let's look at how the library is used

const one = {
  name: 'Macbook Pro',
  specs: {
    ram: '8GB',
    cpu: {
      arch: 'arm',
      cores: 8,
    },
  },
};

const two = {
  name: 'Macbook Pro',
  specs: {
    ram: '8GB',
    cpu: {
      arch: 'arm',
      cores: 8,
    },
  },
};

// Using Lodash
_.isEqual(one, two); // true

Lodash is a really powerful library, I advise you to take a brief look at its documentation, just check the methods and the examples. You may find something that will be super helpful for you in the future.