Dmytro Morar
JavaScript

== vs ===

The == and === operators compare values in JavaScript, but differ in whether type conversion is performed before the comparison. == performs implicit conversion, while === requires a strict match of both type and value.

How it works

JavaScript can automatically or manually convert values from one type to another (type coercion). This affects the result when using the comparison operators.

When conversion occurs

  • Implicit: Happens automatically during operations with different types: '5' + 2 → "52", 5 + '2' → "52", 5 * '2' → 10.
  • Explicit: When conversion is performed manually: Number('5'), String(123), Boolean(0).

Key Differences

  • == compares values with type conversion. Example: 5 == '5' // true
  • === compares values without type conversion (strict comparison). Example: 5 === '5' // false
  • It is recommended to always use === to avoid non-obvious results and logic errors.

Links

  • MDN: Equality comparisons and sameness

On this page