HOWTO · JavaScript
How to Compare Objects in JavaScript
We can compare objects manually or by using the JSON.stringify() function in JavaScript.
On this page
JavaScript’s strict equality operator (===) compares objects by reference: two separately created objects are unequal even when their properties match. To compare contents, choose a method that matches the data and the fields that matter. JSON.stringify() compares serialized text, a manual check can focus on selected properties, and an Object.keys() loop can compare all top-level own enumerable string-keyed properties.
What Object Equality Means in JavaScript
For primitive values such as strings and numbers, === compares values. For objects, it checks whether both variables refer to the same object. Assigning one object to another variable preserves that reference, while creating another object with the same properties does not. This distinction matters whenever a comparison is meant to describe data rather than object identity.
Compare Objects with JSON.stringify()
For simple JSON-compatible data, JSON.stringify() converts each object to text, which you can compare with ===. Matching properties in the same order produce matching serialized text. This is a useful short check when the data shape and property order are controlled.
Run this self-contained example:
const first = { name: 'Apple', price: 2 };
const same = { name: 'Apple', price: 2 };
const reordered = { price: 2, name: 'Apple' };
console.log(JSON.stringify(first) === JSON.stringify(same));
console.log(JSON.stringify(first) === JSON.stringify(reordered));
This comparison is sensitive to property order and is not a general deep-equality test. Serialization also has JSON-specific behavior: for example, object properties whose values are undefined are omitted, and a circular reference cannot be serialized.
Compare Selected Properties Manually
When only particular fields define a match, compare those fields directly. This avoids making unrelated data part of the decision, and the function makes its comparison rule explicit. The example below considers name and price; changes to other properties do not affect its result. Extend the condition when another field is part of your definition of equality.
function sameFruit(a, b) {
return a.name === b.name && a.price === b.price;
}
const first = { name: 'Apple', price: 2, color: 'red' };
const second = { name: 'Apple', price: 2, color: 'green' };
console.log(sameFruit(first, second));
This is a shallow comparison of the selected values. If a selected value is itself an object, === still checks whether those nested values share a reference; it does not compare their contents recursively.
Compare All Own Enumerable Properties
For objects with a changing set of top-level fields, compare their own enumerable string-keyed properties and values. Object.keys() omits symbol-keyed properties, even when those symbols are enumerable, so this method does not compare them. Check the key counts, verify that each key from the first object is an own property of the second, and then compare the corresponding values. The membership check is important: equal key counts alone can mistakenly match different keys whose values are both undefined.
function sameOwnValues(a, b) {
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
for (const key of keysA) {
if (!Object.hasOwn(b, key) || a[key] !== b[key]) return false;
}
return true;
}
const first = { name: 'Apple', price: 2 };
const second = { price: 2, name: 'Apple' };
const differentKeys = { name: 'Apple', color: undefined };
console.log(sameOwnValues(first, second));
console.log(sameOwnValues({ name: undefined }, differentKeys));
Object.keys() returns own enumerable string-keyed properties; it ignores inherited and non-enumerable properties as well as all symbol-keyed properties. This method compares values only one level deep, so nested objects are compared by reference, and values such as NaN do not match themselves under strict equality. Object.hasOwn() is available in modern JavaScript; on older environments, use Object.prototype.hasOwnProperty.call(b, key) for the membership check.
Choose a Comparison Method
Use JSON.stringify() for controlled, JSON-compatible data when property order is consistent and a compact comparison is sufficient. Compare selected properties when only a known subset defines a match. Use the key-and-value loop when all own enumerable top-level string-keyed properties should match regardless of insertion order; enumerable symbol-keyed properties are outside its comparison. If nested structures, special values, or application-specific types need value-based equality, define those rules explicitly or use a dedicated deep-comparison implementation rather than treating any of these shallow checks as universal object equality.