Skip to content

equality operators (==, ===)

In JavaScript, the equality operators (==, ===) compare two values and return a Boolean. The loose form converts operands of different types before comparing, and the strict form never converts them.

Loose equality (==) and its negation (!=) convert mismatched operands, so "42" == 42 returns true. Strict equality (===) and its negation (!==) treat values of different types as unequal. Both forms compare objects by identity and treat NaN as unequal to itself.

Object.is() and SameValueZero, the algorithm behind Array.prototype.includes(), are related comparisons with their own rules for NaN.

JavaScript Equality Operators Syntax

Each operator sits between two operands:

Language: JavaScript Syntax
<left> == <right>
<left> != <right>
<left> === <right>
<left> !== <right>

The four operators form two pairs:

Operator Name Example Result
== Loose equality "1" == 1 true
!= Loose inequality "1" != 1 false
=== Strict equality "1" === 1 false
!== Strict inequality "1" !== 1 true

Each inequality operator returns the opposite of its equality operator for the same operands.

JavaScript Equality Operators Examples

A form field returns the string "42", and the code compares it with a stored number:

Language: JavaScript
> const quantity = "42";
undefined

> quantity == 42
true
> quantity === 42
false

> Number(quantity) === 42
true

The == check converts the string to a number first. The === check returns false because the types differ. An explicit Number() conversion makes the types match while keeping the comparison strict.

Loose equality also groups null with undefined, while NaN matches nothing:

Language: JavaScript
> const middleName = null;
undefined

> middleName == undefined
true
> middleName === undefined
false

> NaN === NaN
false
> Object.is(NaN, NaN)
true

JavaScript Equality Operators Gotchas

These loose-equality results surprise people:

Expression Result Why
"" == 0 true The empty string converts to 0
"0" == false true Both sides convert to the number 0
[] == false true false converts to 0, and the array converts to "", then to 0
null == 0 false null only loosely equals null and undefined
{} == {} false Two separate objects are never equal

Every row gives false with === because strict equality doesn’t convert types.

JavaScript Equality Operators Use Cases

  • Comparing user input or IDs without unintended type conversion
  • Checking for both null and undefined in one test with == null
  • Testing whether two variables reference the same object
  • Detecting a changed value before re-rendering or saving

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 12, 2026