== vs === in JavaScript
The difference between loose equality (==) and strict equality (===). One of the most common sources of bugs for JavaScript beginners.
| Feature | == (Loose) | === (Strict) |
|---|---|---|
| "5" == 5 | true | |
| 0 == false | true | |
| "" == false | true | |
| Type coercion | Yes, converts types before comparing | No, compares value AND type |
| Predictability | Low (many edge cases) | High (no surprises) |
| Recommendation | Avoid except for null checks | Always use this |
| null == undefined | true |
Verdict
Always use === (strict equality). The only acceptable use of == is checking for null/undefined with val == null.
Code Example
Related Tutorials
Related Glossary Terms
In JavaScript, every value is either truthy or falsy when evaluated in a boolean context. Falsy values are: false, 0, '', null, undefined, NaN, and 0n. Everything else is truthy, including empty arrays and objects.
Type CoercionJavaScript's automatic conversion of values from one type to another during operations. Implicit coercion happens with operators like ==, +, and in boolean contexts. Explicit coercion uses functions like Number(), String(), Boolean().
Strict EqualityThe === operator that checks both value and type without type coercion. Unlike == (loose equality), strict equality does not convert operands before comparing. Always prefer === over ==.