Skip to main content

Comparison Operators

Equality Operators

OperatorNameDescription
===Strict equalityReturns true if value and type are equal
!==Strict inequalityReturns true if value or type differ
==Loose equalityReturns true if values are equal after type coercion
!=Loose inequalityReturns true if values differ after type coercion

Examples

1 === 1 // true
1 === "1" // false (different types)

1 == "1" // true (string is coerced to number)
0 == false // true
null == undefined // true

Important: Always prefer === and !== over == and != to avoid unexpected type coercion.

Relational Operators

OperatorNameExample
>Greater than5 > 3 => true
<Less than5 < 3 => false
>=Greater than or equal5 >= 5 => true
<=Less than or equal3 <= 5 => true

Nullish and Special Comparisons

ExpressionResultReason
NaN === NaNfalseNaN is not equal to anything
null === undefinedfalseDifferent types
null == undefinedtrueSpecial rule in loose equality
null == 0falsenull only loosely equals undefined

Operator Precedence

Operators with a higher precedence are evaluated first. Operators on the same level are evaluated left to right.

PrecedenceOperatorDescription
1 (highest)()Grouping
2!Logical NOT
3>, <, >=, <=Relational
4===, !==, ==, !=Equality
5&&Logical AND
6||Logical OR
7 (lowest)??Nullish coalescing

Example

const result = a > 0 && b === 1 || c !== 2;

// && binds tighter than ||, so this is equivalent to:
const result = (a > 0 && b === 1) || c !== 2;

// NOT:
const result = a > 0 && (b === 1 || c !== 2);

// Solution:
const result = a > 0 && (b === 1 || c !== 2);

Tip: When in doubt, use parentheses () to make the intended evaluation order explicit.