== 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" == 5true
0 == falsetrue
"" == falsetrue
Type coercionYes, converts types before comparingNo, compares value AND type
PredictabilityLow (many edge cases)High (no surprises)
RecommendationAvoid except for null checksAlways use this
null == undefinedtrue

Verdict

Always use === (strict equality). The only acceptable use of == is checking for null/undefined with val == null.

Code Example

Javascript
Tip: Modify the code above and click “Run” to see the results

Related Tutorials

Related Glossary Terms