Hello again!
In our last lesson, we introduced the full range of comparison operators and established a "golden rule": always prefer strict equality (===) over loose equality (==). We learned that === is safer because it checks for both value and type, while == can lead to unexpected results because it performs automatic type coercion.
Today, we're going to do a deep dive into that very concept. This lesson is all about understanding the why behind the rule. We'll pull back the curtain on loose equality to see exactly how it works, why it behaves so unpredictably, and why this knowledge is critical for writing the kind of robust, bug-free code required for building applications with TypeScript and Next.js.
By the end of this lesson, you won't just be following a rule; you'll understand the fundamental mechanics that make the rule necessary.
What Is Loose Equality and Type Coercion?
As a quick refresher, strict equality (===) is simple: if the types are different, the result is false. End of story.
Loose equality (==) is more complicated. If the types are different, it doesn't give up. It tries to be "helpful" by converting one or both of the values to a matching type before making the comparison. This automatic conversion process is called type coercion.
While sometimes useful, this "help" often leads to confusing situations. Let's watch a video that perfectly demonstrates some of the strange bugs this can cause.
The video 'JavaScript == VS ===' from the fantastic Web Dev Simplified channel clearly shows why loose equality can be problematic.
Please watch from the beginning to 01:58. Pay close attention to the examples comparing 0 to an empty string ('') and 0 to false. These are classic cases where == gives a surprising true result.
As the video shows, is 0 really the same as false? Or an empty string? Logically, no. But because of type coercion, == says they are. This is the kind of hidden "magic" that can cause major headaches when debugging.
This infographic provides another great visual summary of the core problem.

Under the Hood: The Abstract Equality Algorithm
So, how does JavaScript decide what to convert when you use ==? It follows a formal set of rules defined in the official language specification (ECMAScript). These rules are collectively known as the Abstract Equality Comparison Algorithm.
You don't need to memorize these rules, but understanding that they existβand that they are complexβis key to appreciating why === is superior.
The article 'Equality: == vs ===' from the '33 JavaScript Concepts' series provides an excellent deep dive with great analogies and visuals.
Please read the following sections: Start with "The Teacher Grading Papers: A Real-World Analogy". This provides a brilliant mental model for loose vs. strict. Next, read the section "Loose Equality (==): The Relaxed Comparison" down to (but not including) the "Visual: The Coercion Decision Tree". The goal here is not to memorize the algorithm's steps, but to see how many different rules there are for converting strings, booleans, and objects. Pay special attention to the examples in the "Surprising Results Gallery".
The key takeaway from that reading is that the rules for == are not always intuitive. For example, true == "1" is true because true becomes 1 and "1" becomes 1. However, true == "true" is false because true becomes 1 while the string "true" cannot be converted into a meaningful number, so it becomes NaN (Not a Number), and 1 does not equal NaN.
This complexity is precisely why we avoid it.
Test your understanding!
One of the most famous "weird" JavaScript comparisons is [] == ![]. We know this evaluates to true. Based on the coercion rules you just read about, can you trace the steps that lead to this result?
Hint: Remember the order of operations. What happens to ![] first? Then, what happens when you compare an object ([]) to a boolean (false)?
Show answer
Here is the step-by-step breakdown:
![]is evaluated first. The!operator checks for truthiness. An empty array[]is an object, and all objects are "truthy". Therefore,![]evaluates tofalse. The expression is now[] == false.- A boolean is being compared. The algorithm states that if a boolean is part of a loose comparison, it is converted to a number.
falsebecomes0. The expression is now[] == 0. - An object is being compared to a primitive. The algorithm now converts the object (the empty array
[]) to a primitive. For an array, this means calling itstoString()method.[].toString()results in an empty string"". The expression is now"" == 0. - A string is being compared to a number. The algorithm converts the string to a number. The empty string
""becomes0. The expression is now0 == 0. - Final Comparison:
0 == 0is, finally,true.
This complex chain of conversions perfectly illustrates why relying on == is risky!
The One Acceptable Exception: Checking for null and undefined
Despite everything we've said, there is one scenario where developers sometimes intentionally use ==. The loose equality algorithm has a special rule: null is only loosely equal to undefined (and itself), and nothing else.
This creates a convenient shortcut for checking if a variable is either null or undefined.
Let's return to the Web Dev Simplified video, which explains this exception perfectly.
Let's revisit the 'JavaScript == VS ===' video to see the one practical use case for loose equality.
Please watch the segment from 01:58 to 02:46. The instructor explains how value == null can be used to check for both null and undefined at the same time.
Using x == null is equivalent to writing the more verbose x === null || x === undefined. Because this is a well-known and deliberate exception, it's generally considered safe to use. However, for consistency, many developers still prefer to be explicit and use the === version.
Strict Equality (===): Simple and Predictable
Now, let's contrast all that complexity with the simple beauty of strict equality (===).
Let's go back to the 'Equality: == vs ===' article one last time to review the rules for our preferred operator.
Read the section "Strict Equality (===): The Reliable Choice". Notice how much shorter and more straightforward the algorithm is. Also, pay close attention to the subsection "Object Comparison: Reference vs Value". This is a critical concept we will build on later.
The rules for === are simple:
- Are the types different? If yes,
false. - Are the types the same? Compare the values.
That's it. No coercion, no surprises. The point about object comparison is especially important for your journey toward Next.js and React. Two different objects or arrays are never equal with ===, even if they contain the same data, because === checks if they are the exact same object in memory.
Conclusion
You've done an excellent job digging into one of JavaScript's most notorious features. By understanding how loose equality works, you've armed yourself with the knowledge to write more predictable and professional code.
Key Takeaways:
- Loose Equality (
==) uses the Abstract Equality Comparison Algorithm to perform implicit type coercion, converting values before comparing them. - This coercion follows a complex set of rules that can lead to unexpected and buggy behavior (e.g.,
[] == ![]istrue). - Strict Equality (
===) is predictable. It does not perform type coercion and returnsfalseimmediately if the types are different. - The Golden Rule remains: Always use
===. - The only common exception is using
value == nullas a shortcut to check if a value is eithernullorundefined. - Objects and arrays are compared by reference, not by their contents.
{} === {}isfalse.
Next Up:
We can now reliably compare values to get true or false. In the next lesson, we'll learn how to combine multiple conditions using logical operators (&&, ||, !). This will allow us to build more complex decision-making logic, asking questions like "is x greater than 5 AND is y less than 10?"