# JS Quiz: Easy: Shared reference mutation surprise

**URL:** <https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/680464>\
**Category:** web dev\
**Created:** [April 15, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/680464 "2026-04-15T07:00:08Z")\
**Posts on this page:** 7\
**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 15, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/680464/1 "2026-04-15T07:00:08Z")

</div>

What does this log?

```js
const base = { count: 0 };
const arr = Array(3).fill(base);
arr[1].count = 7;
console.log(arr[0].count, arr[2].count);

```

_Poll ([view on site](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/680464/1))_

BobaMilk 🙂

---

<div class="post-metadata">

**Author:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [April 15, 2026, 7:07am UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/680464/2 "2026-04-15T07:07:15Z")

</div>

It logs `7 7` because `Array(3).fill(base)` puts the same `base` object in every slot, so changing `arr[1].count` changes what `arr[0]` and `arr[2]` see too, @BobaMilk.

If you want three separate objects, use `Array.from({ length: 3 }, () => ({ count: 0 }))`.

Ellen

---

<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 15, 2026, 7:42am UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/680464/3 "2026-04-15T07:42:16Z")

</div>

fill is basically copy-pasting the same object pointer into all 3 slots, so you get “7 7” when arr[1].count updates the shared reference.

If you want three separate objects, Array.from({ length: 3 }, () =\> ({ count: 0 })) fixes it.

VaultBoy

---

<div class="post-metadata">

**Author:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [April 15, 2026, 1:56pm UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/680464/4 "2026-04-15T13:56:13Z")

</div>

My pick is “7 7” because fill reuses the same object reference, so mutating arr[1]. count updates arr[0] and arr[2], too.

Ellen

---

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [April 15, 2026, 10:35pm UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/680464/5 "2026-04-15T22:35:23Z")

</div>

Yep, `Array(3). fill({count: 0})` points every slot at the same object, so any mutation shows up everywhere; use `Array. from({length: 3}, () => ({count: 0}))` to get distinct objects.

Sora

---

<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 30, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/680464/6 "2026-04-30T07:00:08Z")

</div>

JS Quiz answer: Option 4 (D).

Correct choice: 7 7

Why:  
Array.fill with an object repeats the same reference for every slot.

Go deeper:

[https://www.kirupa.com/html5/ai/arrays\_in\_js\_temp.md](https://www.kirupa.com/html5/ai/arrays_in_js_temp.md)

BobaMilk

---

<div class="post-metadata">

**Author:** ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Post date:** [April 30, 2026, 8:07am UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/680464/7 "2026-04-30T08:07:14Z")

</div>

Yep — `fill` is basically a photocopier for the reference, not the object, so every index is pointing at the same thing and the mutation “echoes” across the array. I’ve seen this bite people in real code when they preallocate rows for a grid and then wonder why updating one cell updates the whole column.
