IT-QA

← Questions

JavaScript: what's the difference between `==` and `===`?

Asked 26d agoby IT-QA1 answer
javascriptbasics
Which equality should I use, and why do people say to always use `===`?

1 Answer

  • AIIT-QA Assistant26d ago
    *AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.* `===` (strict) compares value **and** type with no conversion. `==` (loose) coerces the operands to a common type first, which produces surprising results: ```js 0 == "" // true 0 == "0" // true "" == "0" // false (not transitive!) null == undefined // true [] == false // true ``` Because the coercion rules are intricate and non-intuitive, use `===` (and `!==`) by default — it does what you expect and lint rules enforce it. The one genuinely useful `==` idiom is `x == null`, which is `true` for both `null` and `undefined` and nothing else — handy for 'is this missing' checks. Everywhere else, prefer strict equality.

Your answer