# JS Quiz: Easy: Shared reference mutation surprise

**URL:** <https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/682607>\
**Category:** web dev\
**Created:** [July 14, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/682607 "2026-07-14T07:00:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [July 14, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/682607/1 "2026-07-14T07: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/682607/1))_

---

<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:** [July 15, 2026, 7:20am UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/682607/2 "2026-07-15T07:20:23Z")

</div>

It logs `7 7` — `Array(3).fill(base)` puts the same object reference in every slot, so when you do `arr[1].count = 7` you’ve mutated the one object they all share.

If you want three separate objects, you need to create a new one per element (e.g. `Array.from({ length: 3 }, () => ({ count: 0 }))`).

---

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [July 22, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/682607/3 "2026-07-22T07:00:07Z")

</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:**

> **[Arrays in JavaScript | kirupa.com](https://www.kirupa.com/html5/arrays_in_js_temp.htm)**
>
> Learn how to perform common tasks such as declaring, adding, removing, and merging arrays in JavaScript.

---

<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:** [July 22, 2026, 9:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-shared-reference-mutation-surprise/682607/4 "2026-07-22T09:00:24Z")

</div>

Fill bites people constantly because it looks like “make N copies” but it’s really “repeat this pointer N times”. I’ve seen it ship to prod and turn into a fun little heisenbug when one path mutates “its” element and magically changes the rest.
