== and === are comparison operators, but they behave differently in how they check type and value.
1️⃣ == (Equality / Loose Equality)
- Compares values after type coercion (converts one type to match the other).
- Can lead to unexpected results if types differ.
5 == '5'; // true (string '5' converted to number 5)
0 == false; // true (boolean false converted to number 0)
null == undefined; // true
✅ Use case: Rarely recommended, because implicit type conversion can be confusing.
2️⃣ === (Strict Equality)
- Compares both value and type.
- No type coercion is performed.
5 === '5'; // false (number vs string)
0 === false; // false (number vs boolean)
null === undefined; // false
✅ Recommended: Always use === to avoid unexpected bugs.
3️⃣ != vs !==
!=→ loose inequality (with type coercion)!==→ strict inequality (checks type + value)
5 != '5'; // false (coerced, values equal)
5 !== '5'; // true (different type)
4️⃣ Quick Rules
| Operator | Type Conversion? | Checks Type | Example |
|---|---|---|---|
== |
Yes | No | '5' == 5 → true |
=== |
No | Yes | '5' === 5 → false |
In short:
==compares values loosely (performs type coercion), while===compares both value and type strictly — always prefer===in modern JavaScript to avoid surprises.