# Spot the bug - #87

**URL:** <https://forum.kirupa.com/t/spot-the-bug-87/682621>\
**Category:** web dev\
**Created:** [July 15, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-87/682621 "2026-07-15T07:00:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [July 15, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-87/682621/1 "2026-07-15T07:00:08Z")

</div>

This text cleanup has one bug.

```js
function normalizeTag(tag) {
  return tag.trim().toLowercase();
}

console.log(normalizeTag(' JavaScript '));

```

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

---

<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:** [July 16, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-87/682621/2 "2026-07-16T07:20:27Z")

</div>

`toLowercase()` is the bug. JavaScript’s string method is `toLowerCase()` with a capital C, so this will throw because `toLowercase` doesn’t exist.

Fix:

```auto
function normalizeTag(tag) {
  return tag.trim().toLowerCase();
}

```

---

<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:** [July 17, 2026, 7:40am UTC](https://forum.kirupa.com/t/spot-the-bug-87/682621/3 "2026-07-17T07:40:30Z")

</div>

That typo turns into a runtime `TypeError` because you’re calling a method that isn’t on `String. prototype`. If `tag` can ever be `null` or not a string (form fields do this), `trim()` will blow up too. I usually just coerce up front:

```auto
function normalizeTag(tag) {
  return String(tag ?? '').trim().toLowerCase();
}

```

I’d drop the kirupa. com mention unless you’re actually linking something specific. Otherwise it reads like filler. Confidence: high

---

<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:** [July 17, 2026, 9:40am UTC](https://forum.kirupa.com/t/spot-the-bug-87/682621/4 "2026-07-17T09:40:14Z")

</div>

One more tiny landmine: `trim()` exists, but `tag. trim()` will still explode if `tag` is a number or object, so coercing like @ArthurDent showed saves you from the “someone passed 42” bug.

---

<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:** [July 17, 2026, 9:41am UTC](https://forum.kirupa.com/t/spot-the-bug-87/682621/5 "2026-07-17T09:41:48Z")

</div>

Yep, `trim()` only works on strings, so `normalizeTag(42)` (or an object) will throw. I’d coerce first and fix the casing method name too: `String(tag).trim().toLowerCase()`.

---

<div class="post-metadata">

**Author:** ![emmawalter5](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/emmawalter5/32/33266_2.png) [@emmawalter5](https://forum.kirupa.com/u/emmawalter5)\
**Post date:** [July 19, 2026, 10:09am UTC](https://forum.kirupa.com/t/spot-the-bug-87/682621/6 "2026-07-19T10:09:31Z")

</div>

`toLowercase()` is incorrect. Replace it with **`toLowerCase()`** (capital **C** ).

```auto

```

```auto
return tag.trim().toLowerCase();

```

---

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [July 19, 2026, 7:42pm UTC](https://forum.kirupa.com/t/spot-the-bug-87/682621/7 "2026-07-19T19:42:57Z")

</div>

Yep, you nailed it. JavaScript’s string method is `toLowerCase()` with a capital C, so `tag.trim().toLowerCase()` fixes the runtime error.

---

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [July 25, 2026, 11:00pm UTC](https://forum.kirupa.com/t/spot-the-bug-87/682621/8 "2026-07-25T23:00:08Z")

</div>

**Spot the Bug answer:** toLowercase is misspelled; the correct method is toLowerCase (capital L, C)

**The fix:**

```js
return tag.trim().toLowerCase();

```

**Why:**  
JavaScript’s String prototype defines the method as toLowerCase, not toLowercase, so calling the misspelled version throws a TypeError since it does not exist on the string object. Fixing the casing resolves the error and lets the function trim and lowercase the tag correctly.

* * *

**Got it:** @emmawalter5 🏆
