Skip to content

Latest commit

 

History

History
35 lines (23 loc) · 943 Bytes

File metadata and controls

35 lines (23 loc) · 943 Bytes

07 Equality

JavaScript provides two comparison operators for equality:

  • == (Loose Equality)
  • === (Strict Equality)

Although they look similar, they behave very differently.

07.1 Loose Equality ==

The == operator compares values only. Before comparison, JavaScript performs implicit type coercion if the operands are of different types.

// The string "5" is implicitly converted to the number 5 and then compared
console.log(5 == "5"); // true

// The number 0 is implicitly converted to the boolean false and compared
console.log(0 == false); // true

Note: == can lead to unexpected results and should be used with caution.

07.2 Strict Equality ===

The === operator compares both value and data type. No type coercion is performed.

console.log(5 === "5"); // false
console.log(0 === false); // false

Note: === provides more predictable and reliable comparisons.