JavaScriptEasy
How do Sets and Maps handle equality checks for objects?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
Sets and Maps in JavaScript handle equality checks for objects based on reference equality, not deep equality. This means that two objects are considered equal only if they reference the same memory location. For example, if you add two different object literals with the same properties to a Set, they will be treated as distinct entries.
js
const set = new Set();
const obj1 = { a: 1 };
const obj2 = { a: 1 };
set.add(obj1);
set.add(obj2);
console.log(set.size); // Output: 2
