Explain type coercion in JavaScript. What is the difference between == and ===, and why do style guides recommend ===?
Show hints
- Try the comparisons in the browser console and look for the one that breaks transitivity.
Show the explanation
Model answer. Type coercion is JavaScript converting a value to another type automatically, for example '5' * 2 gives 10 (string to number), and '5' + 2 gives '52' (number to string, because + with a string concatenates).
===(strict equality) compares type and value with no conversion:5 === '5'isfalse.==(loose equality) first coerces the operands to a common type, following a long rule table:5 == '5'istrue.
Loose equality produces results that are easy to misread:
0 == '' // true
0 == '0' // true
'' == '0' // false (not transitive!)
null == 0 // false
null == undefined // true
[] == false // trueBecause of these, use === and !== by default; convert explicitly when you mean to (Number(input) === 5). The one common, deliberate use of == is x == null, which is true for both null and undefined.
Original UniCircle question. Last reviewed 22 Oct 2026.
