What is the difference between == and ===?
Try code in the playgroundUnderstand the concept
== 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.
Important ideas
-
==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 |
Can you explain it without the notes?
What is the difference between == and ===?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
== 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.
Turn this concept into a clear answer.
- 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 ===?”. Examples are study fragments; framework behavior can depend on your version.