== vs ===
== - loose (abstract) comparison
== converts operand types before comparing them.
In other words, JavaScript tries to "guess" whether the values are equal after type coercion.
Examples:
javascript
5 == '5' // true → the string '5' is converted to the number 5
0 == false // true → false is coerced to 0
null == undefined // true → this is the only case where they are equal
' ' == 0 // true → an empty string is converted to 0Danger: implicit conversions often cause bugs, especially when validating user input or data from an API.
=== - strict comparison
=== does not perform type conversion.
Operands must be of the same type and value for the result to be true.
Examples:
javascript
5 === '5' // false → different types (number and string)
0 === false // false → different types
null === undefined // false → different types
5 === 5 // true → same value and typeSummary: when to use what
Recommendation:
Always use === (and !==) unless you have a specific reason to use a loose comparison. This makes code predictable and reduces the number of bugs.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.