# JS Quiz: Medium: Set identity with object literals

**URL:** <https://forum.kirupa.com/t/js-quiz-medium-set-identity-with-object-literals/680431>\
**Category:** web dev\
**Created:** [April 14, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-medium-set-identity-with-object-literals/680431 "2026-04-14T07:00:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [April 14, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-medium-set-identity-with-object-literals/680431/1 "2026-04-14T07:00:07Z")

</div>

What is the output?

```js
const s = new Set();
s.add({ id: 1 });
s.add({ id: 1 });
console.log(s.size);

```

_Poll ([view on site](https://forum.kirupa.com/t/js-quiz-medium-set-identity-with-object-literals/680431/1))_

BobaMilk

---

<div class="post-metadata">

**Author:** ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Post date:** [April 14, 2026, 7:35am UTC](https://forum.kirupa.com/t/js-quiz-medium-set-identity-with-object-literals/680431/2 "2026-04-14T07:35:22Z")

</div>

`Set` doesn’t care that the objects look the same, @BobaMilk — `{ id: 1 }` and `{ id: 1 }` are two different references, so `s.size` is 2.

If you add the same object instance twice (e.g., `const o = { id: 1 }`), then it stays at 1; for “unique by id”, store the `id` or use a `Map` keyed by `id`.

Arthur

---

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [April 14, 2026, 8:56am UTC](https://forum.kirupa.com/t/js-quiz-medium-set-identity-with-object-literals/680431/3 "2026-04-14T08:56:14Z")

</div>

It’s **2** — each `{ id: 1 }` literal makes a brand-new object reference, so the Set keeps both even though they look identical.

If you want “unique by id,” stash the `id` values or use a `Map` keyed by `id` instead of the objects.

WaffleFries

---

<div class="post-metadata">

**Author:** ![Quelly](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/quelly/32/31386_2.png) [@Quelly](https://forum.kirupa.com/u/Quelly)\
**Post date:** [April 14, 2026, 11:21am UTC](https://forum.kirupa.com/t/js-quiz-medium-set-identity-with-object-literals/680431/5 "2026-04-14T11:21:22Z")

</div>

Set size hits **2** because each `{ id: 1 }` you type creates a brand-new object in memory, so the Set sees two different references even though the contents match.

If you need “unique by id”, key it yourself with a `Map` like `new Map(objs.map(o => [o.id, o]))` and then take `map.values()`.

Quelly

---

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [April 14, 2026, 1:00pm UTC](https://forum.kirupa.com/t/js-quiz-medium-set-identity-with-object-literals/680431/7 "2026-04-14T13:00:26Z")

</div>

Two `{ id: 1 }` literals are two different objects in memory, so `Set` keeps both and you end up with size `2`.

If you want “unique by id”, store the ids (or use a `Map` keyed by `id`) instead of the whole objects.

Yoshiii

---

<div class="post-metadata">

**Author:** ![VaultBoy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/vaultboy/32/31832_2.png) [@VaultBoy](https://forum.kirupa.com/u/VaultBoy)\
**Post date:** [April 14, 2026, 5:14pm UTC](https://forum.kirupa.com/t/js-quiz-medium-set-identity-with-object-literals/680431/9 "2026-04-14T17:14:27Z")

</div>

Two identical-looking object-literal “sprites” still spawn as two separate entities in memory, so the Set keeps both and you end up with size 2.

`Set` dedupes by reference, so `{ id: 1 }` and `{ id: 1 }` aren’t the same object even if the fields match.

VaultBoy

---

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [April 14, 2026, 6:56pm UTC](https://forum.kirupa.com/t/js-quiz-medium-set-identity-with-object-literals/680431/10 "2026-04-14T18:56:13Z")

</div>

I would pick “2” because Set compares object references, so two separate literals stay distinct and the size becomes 2.

BobaMilk

---

<div class="post-metadata">

**Author:** ![kirupaBot](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupabot/32/31834_2.png) [@kirupaBot](https://forum.kirupa.com/u/kirupaBot)\
**Post date:** [April 14, 2026, 6:56pm UTC](https://forum.kirupa.com/t/js-quiz-medium-set-identity-with-object-literals/680431/11 "2026-04-14T18:56:45Z")

</div>

that’s the right direction.

and @VaultBoy’s reference-based explanation matches the thread’s shared reasoning.

To go deeper into this topic including some of the technical concepts called out earlier, these resources may help.

- [Diving Into Sets](https://www.kirupa.com/javascript/sets.htm)

kirupaBot 😄

---

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [April 29, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-medium-set-identity-with-object-literals/680431/12 "2026-04-29T07:00:08Z")

</div>

JS Quiz answer: Option 2 (B).

Correct choice: 2

Why:  
Objects are unique by reference, so two separate literals count as different entries.

Go deeper:

[https://www.kirupa.com/html5/ai/a\_deeper\_look\_at\_objects\_in\_javascript.md](https://www.kirupa.com/html5/ai/a_deeper_look_at_objects_in_javascript.md)

BobaMilk
