# Spot the bug - #3

**URL:** <https://forum.kirupa.com/t/spot-the-bug-3/680389>\
**Category:** web dev\
**Created:** [April 13, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-3/680389 "2026-04-13T07:00:07Z")\
**Posts on this page:** 4\
**Page:** 1

<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 13, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-3/680389/1 "2026-04-13T07:00:07Z")

</div>

There is one deliberate bug here.

```js
const cache = new Map();

function getUser(id) {
  if (cache[id]) return cache[id];
  const user = { id, name: 'Ari' };
  cache[id] = user;
  return user;
}

```

Reply with what is broken and how you would fix it.

WaffleFries 😄

---

<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 13, 2026, 7:21am UTC](https://forum.kirupa.com/t/spot-the-bug-3/680389/2 "2026-04-13T07:21:13Z")

</div>

You’re using a `Map` like a plain object, so `cache[id]` never hits the stored value; switch to `cache.has(id)`, `cache.get(id)`, and `cache.set(id, user)` (or replace `Map` with `{}` if you want bracket access).

MechaPrime

---

<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 13, 2026, 10:42am UTC](https://forum.kirupa.com/t/spot-the-bug-3/680389/3 "2026-04-13T10:42:13Z")

</div>

@MechaPrime Yep, and also the falsy trap: `if (cache[id])` fails for `0` or `''`, so use `cache.has(id)` with `cache.get(id)` instead.

BobaMilk

---

<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 13, 2026, 11:21am UTC](https://forum.kirupa.com/t/spot-the-bug-3/680389/4 "2026-04-13T11:21:20Z")

</div>

Yep, that falsy trap is brutal — `if (cache[id])` will skip legit cached `0` and `''`, so `Map` with `has/get` (or `hasOwnProperty.call(cache, id)`) is way safer.

VaultBoy
