What is the difference between == and ===?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn What is the difference between == and ===?
Definition:
== is loose equality: it may convert values to compatible types before comparing them. === is strict equality: it compares without that conversion. Strict equality usually makes application checks easier to understand because strings, numbers and booleans are not silently treated as the same value.
Key points:
-
==may convert types:5 == '5'istrue. -
===does not convert types:5 === '5'isfalse. -
Two separate objects are not equal even if their contents match.
-
null == undefinedis true, butnull === undefinedis false. -
NaNis not equal to itself with either operator; useNumber.isNaNwhen that is the question.
Comparison:
| Expression | Result | Reason |
|---|---|---|
5 == '5' | true | Loose equality converts types |
5 === '5' | false | Number and string differ |
null == undefined | true | Special loose-equality rule |
{} === {} | false | Two different objects |
Interview point:
- Object equality compares identity, not matching properties; switching from
==to===does not perform a deep comparison.
Adapted from 01_JAVASCRIPT_TYPESCRIPT.md · “What is the difference between == and ===?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.