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

17 viewsSkills Development

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

1. == (Equality Operator)

  • Performs type coercion before comparison.
  • The operation converts different operand types before performing value comparison.
  • The use of implicit type conversion leads to unexpected results due to this behavior.

Example:
console.log(5 == “5”); // true (string “5” is converted to number 5)

console.log(0 == false); // true (false is converted to 0)

console.log(null == undefined); // true

console.log([] == false); // true ([] is converted to an empty string, then to 0)

2. === (Strict Equality Operator)

  • Does not perform type coercion.
  • Checks both value and type.
  • Users should choose this type due to its high predictability in enforcing strict comparison protocols.

Example:
console.log(5 === “5”); // false (different types: number vs string)

console.log(0 === false); // false (number vs boolean)

console.log(null === undefined); // false (different types)

console.log([] === false); // false ([] is an object, false is a boolean)

  • The use of === should be prioritized because it helps prevent accidental type conversions.
  • Apply a double equality sign exclusively for cases requiring intentional type conversion.
Abarna Vijayarathinam Asked question 7 hours ago
0