JavaScript Comparison Operators

Subject: JavaScript

Comparison operators in JavaScript are used to compare two values. These comparisons return a Boolean value: either true or false. They are essential for controlling program flow using if, else, switch, and loop conditions.


Common JavaScript Comparison Operators

OperatorNameDescription
==Equal toReturns true if values are equal (loose)
===Strict equal toReturns true if values and types match
!=Not equal toReturns true if values are not equal
!==Strict not equal toReturns true if values or types differ
>Greater thanReturns true if left is greater
<Less thanReturns true if left is smaller
>=Greater than or equal toReturns true if left >= right
<=Less than or equal toReturns true if left <= right

Example: Comparison Operators in JavaScript


Loose vs Strict Equality

  • == compares only values, ignoring data types.
  • === compares both values and data types.

When to Use Each Operator

  • Use === and !== in modern JavaScript to avoid type coercion.
  • Use <, >, <=, >= to compare numeric values or dates.
  • Use == only when you understand and require type conversion flexibility.

Key Takeaways

  • Comparison operators return Boolean values (true or false).
  • Prefer strict equality (===, !==) to avoid unexpected type coercion.
  • Use comparison operators to control logic in conditionals and loops.
  • Misusing == can introduce subtle bugs—understand JavaScript's type coercion rules.
Next : Logical Operators