JavaScript
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
10: Strict Equality vs Loose Equality
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.");
}There are no comments for now.