Skip to Content
Course content

10: Strict Equality vs Loose Equality

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've spent a lot of time debugging code where the logic looked perfect on paper, but the program was behaving like it had a mind of its own. Nine times out of ten, the culprit was a "loose equality" check. In JavaScript, there's a massive difference between saying something is roughly the same and saying it is exactly the same.

The "Out of Stock" Trigger

Let's build a tiny piece of logic for an e-commerce site. We want to check the quantity of an item in the warehouse. If the quantity is 0, we need to display an "Out of Stock" message to the user. Here is how I'd start writing that function:

function checkStock(quantity) {
  if (quantity == 0) {
    return "Out of Stock";
  }
  return "Available";
}

At first glance, this looks fine. If I pass in 0, I get "Out of Stock". If I pass in 5, I get "Available". I'm using the loose equality operator (==), which tells JavaScript: "Check if these two things are equal, and if they aren't the same type, try to convert one of them so they match."

Where the Shortcut Backfires

Here is where I messed up. In a real app, the quantity value often comes from an HTML input field or an API response, meaning it might arrive as a string instead of a number. I thought using == was a clever shortcut because it would treat the string "0" as the number 0.

But look what happens when the user leaves the input field completely empty, sending us an empty string:"".

console.log(checkStock("0")); // "Out of Stock" (Seems fine)
console.log(checkStock(""));   // "Out of Stock" (Wait, what?)

Because I used ==, JavaScript performed "type coercion." It looked at the empty string "" and the number 0 and decided that, for the sake of this comparison, they were equivalent. This is a disaster. An empty input field should probably trigger a "Please enter a value" warning, not tell the customer the item is sold out.

Locking it Down with Strict Equality

To fix this, I need to stop letting JavaScript guess my intentions. I'll switch to the strict equality operator (===). This operator doesn't do any coercion. If the types are different (e.g., one is a string and one is a number), it immediately returns false without trying to be "helpful."

Here is the corrected version of my function:

function checkStock(quantity) {
  // Now we check for both value AND type
  if (quantity === 0) {
    return "Out of Stock";
  }
  return "Available";
}

Now, if I pass in "", it returns "Available" (or I can add a separate check for empty strings). More importantly, if I pass in the string "0", it also returns "Available".

You might think, "But now I have to manually convert my strings to numbers!" Exactly. And that's actually a good thing. By explicitly calling Number(quantity) before the comparison, you are documenting your intent. You're telling anyone reading your code—including your future self—exactly what data type you expect to be dealing with.

My rule of thumb? Always use ===. If you find yourself wanting to use == because it's "easier," that's usually a sign that you're ignoring a potential bug in your data flow.




📋 Practical Task

The Shopping Cart Total Validator

You are building a checkout page. You have a variable called cartTotal that is being pulled from a database. You need to write a condition that applies a "Free Shipping" badge, but only if the cartTotal is exactly 0 (for a promotional gift cart).

The current code is using loose equality, which is causing a bug: when the cartTotal is false (due to a database error), the user is incorrectly getting free shipping.

Your Task: Fix the code below by changing the equality operator to ensure that only the actual number 0 triggers the free shipping badge.

const cartTotal = false; // This is the bugged value from the DB

if (cartTotal == 0) {
  console.log("Free Shipping Applied!");
} else {
  console.log("Standard Shipping Rates Apply.");
}
Rating
0 0

There are no comments for now.

to be the first to leave a comment.