TechnicalEasy JavaScript
What is the difference between == and === in JavaScript?
Asked for Frontend Developer, Full-Stack Developer, Mobile Developer
Why they ask this: Loose equality causes subtle bugs; interviewers want to see that you default to strict comparison.
Answer
===(strict equality) compares value and type. No conversion happens.==(loose equality) converts the operands to a common type first, then compares.
js
0 == '0' // true (string converted to number)
0 === '0' // false (number vs string)
null == undefined // true
null === undefined// false
'' == 0 // trueAlways use === (and !==) unless you deliberately want conversion. A common, accepted exception is value == null, which checks for both null and undefined.
Can you explain this in your own words?