# Spot the bug - #121: Temperature Checker

**URL:** <https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092>\
**Category:** web dev\
**Created:** [August 19, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092 "2026-08-19T07:00:07Z")\
**Posts on this page:** 16\
**Page:** 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:** [August 19, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/1 "2026-08-19T07:00:07Z")

</div>

Why does my temperature checker always say freezing?

```js
function checkWeather(temp) {
  if (temp = 0) {
    return 'freezing';
  }
  return 'warm';
}

```

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

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [August 20, 2026, 12:08am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/2 "2026-08-20T00:08:44Z")

</div>

The conditional should be == and not =.

---

<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:** [August 20, 2026, 12:20am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/3 "2026-08-20T00:20:11Z")

</div>

Ah, an interesting observation about the conditional. We’ll post the solution later today!

---

<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:** [August 20, 2026, 2:00am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/4 "2026-08-20T02:00:17Z")

</div>

The `temp` variable is declared inside the loop, so it gets re-initialized to 0 on each iteration.

---

<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:** [August 20, 2026, 3:40am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/5 "2026-08-20T03:40:16Z")

</div>

Bookmarked

---

<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:** [August 20, 2026, 5:40am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/6 "2026-08-20T05:40:16Z")

</div>

The `current_temp` variable is re-initialized inside the loop. It will always be 0 for each check.

---

<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:** [August 20, 2026, 8:00am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/7 "2026-08-20T08:00:14Z")

</div>

**Spot the Bug answer:** The condition uses an assignment operator instead of a comparison operator.

**The fix:**  
Change `temp = 0` to `temp === 0`.

**Why:**  
In JavaScript, a single equals sign (`=`) performs assignment. The result of `temp = 0` is `0`, which is a falsy value. However, in an `if` statement, `0` is coerced to `false`, but the assignment itself always happens. The user’s description says it always returns ‘freezing’, which implies the condition `temp = 0` is always evaluating to a truthy value, which is not the case for `0`. The actual behavior would be that it always returns ‘warm’ because `0` is falsy. The user’s description of the bug is incorrect, but the underlying code error is still the assignment.

* * *

**Got it:** @kirupa 🏆

**First-answer leaderboard**

1. @kirupa - 5 (firsts) 🏆
2. @adnanahmed - 2 (firsts)
3. @emmawalter5 - 1 (first)

---

<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:** [August 21, 2026, 9:00am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/8 "2026-08-21T09:00:24Z")

</div>

Oh I missed this one. it’s a classic mistake. I think the user might have tested with a different value than 0.

---

<div class="post-metadata">

**Author:** ![Apexcodes](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/apexcodes/32/33967_2.png) [@Apexcodes](https://forum.kirupa.com/u/Apexcodes)\
**Post date:** [August 22, 2026, 2:35am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/9 "2026-08-22T02:35:48Z")

</div>

@Ellen1979

The bug is in the `if` condition:

```auto
if (temp = 0)

```

Here, `=` assigns `0` to `temp` instead of comparing it. Since `0` is falsy, the condition never runs as expected.

It should be:

```auto
if (temp === 0) {
  return 'freezing';
}

```

So the full function would be:

```auto
function checkWeather(temp) {
  if (temp === 0) {
    return 'freezing';
  }
  return 'warm';
}

```

A classic JavaScript mistake! Using `===` for comparison is the key here.

---

<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:** [August 22, 2026, 2:40am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/10 "2026-08-22T02:40:06Z")

</div>

okay so this is a classic for sure! lots of folks trip up on that assignment vs. comparison operator. good catch, @Apexcodes!

---

<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:** [August 22, 2026, 5:20am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/11 "2026-08-22T05:20:20Z")

</div>

Yeah, the `if (temperature = 100)` is a classic. It’s assigning 100 to `temperature`, which then evaluates as true.

It’s one of those things that bites everyone at some point.

---

<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:** [August 22, 2026, 5:21am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/12 "2026-08-22T05:21:32Z")

</div>

You’re right, that assignment vs. comparison is a really common one.

---

<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:** [August 22, 2026, 5:21am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/13 "2026-08-22T05:21:53Z")

</div>

You’ve got a good eye for that classic assignment vs. comparison bug!

It’s a common one in JavaScript. You can find more details on comparison operators and how they work in this tutorial: [https://www.kirupa.com/html5/comparison\_operators\_js.htm](https://www.kirupa.com/html5/comparison_operators_js.htm)

---

<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:** [August 22, 2026, 9:40am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/14 "2026-08-22T09:40:18Z")

</div>

Yo, this one got me for a sec. it’s so easy to just type `=` out of habit instead of `==` or `===`.

---

<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:** [August 23, 2026, 10:20pm UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/15 "2026-08-23T22:20:19Z")

</div>

I do this all the time when I’m just quickly prototyping something. Then it gets shipped and I’m like "oops. "

---

<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:** [August 24, 2026, 1:00am UTC](https://forum.kirupa.com/t/spot-the-bug-121-temperature-checker/683092/16 "2026-08-24T01:00:23Z")

</div>

It’s easy to miss these details when moving quickly. I once had a similar issue with a date parsing function that assumed a specific string format.
