What is the difference between == and === in JavaScript?

EasyPhone Screen
GoogleSoftware Engineer
234

Explain the difference between loose equality (==) and strict equality (===) operators in JavaScript. When would you use one over the other?

2 Answers

187
Top Answer

The key difference is type coercion:

Loose Equality (==)

Converts operands to the same type before comparing:

5 == "5"      // true (string converted to number)
null == undefined  // true
0 == false    // true
"" == false   // true

Strict Equality (===)

No type conversion - both value AND type must match:

5 === "5"     // false (different types)
null === undefined  // false
0 === false   // false
"" === false  // false

Best Practice

Always use === unless you have a specific reason to use ==. It prevents subtle bugs from unexpected type coercion.

The only common exception: value == null checks for both null and undefined in one comparison.

JSExpert
45

Quick tip: ESLint has the eqeqeq rule that enforces strict equality. Enable it in your projects:

{
  "rules": {
    "eqeqeq": ["error", "always"]
  }
}

This catches accidental use of == during development.

LintFan

Share Your Answer

Help others by sharing your knowledge and experience with this question.

Coming soon...

Related Questions

View all

More from Google

View all