What is the difference between == and === in JavaScript?
Quick Answer
=== (strict equality) compares value and type with no conversion. == (loose equality) coerces the operands to a common type first, which produces surprising results ('' == 0, '0' == 0, null == undefined are all true). Always use === unless you specifically want x == null to catch both null and undefined.
=== checks type and value with no conversion, so 1 === '1' is false. == converts the operands to a common type first, and that produces a lot of gotchas — empty string equals zero, '0' equals zero, null equals undefined. I use === everywhere. The one exception people accept is x == null as a shorthand that's true for both null and undefined. And separately, NaN isn't equal to itself with either operator, so you check that with Number.isNaN.
1 === '1'→false;1 == '1'→true(string coerced to number).null == undefined→true, butnull === undefined→false.NaN === NaN→false(useNumber.isNaNorObject.is).0 === -0→true, butObject.is(0, -0)→false.- The one accepted
==use isif (x == null)as shorthand for “null or undefined”. Linters flag everything else.