Equality(==) Comparison Operator in JavaScript

Last Updated : 6 Feb, 2026

In JavaScript, the Comparison Equality Operator (==) is used to compare the values of two operands. It returns true if the values are equal and false otherwise. This operator is known as loose equality because it performs automatic type conversion before comparison.

  • Performs type conversion when comparing operands of different types.
  • Returns true only when the values are equal after conversion.
  • When comparing objects, it checks only references, not the actual content of the objects.
JavaScript
// Type conversion (loose equality)
5 == "5"        // true (string is converted to number)

// Different values
10 == 20        // false

// Boolean conversion
true == 1       // true
false == 0      // true

// Object comparison (reference check)
let obj1 = { x: 10 };
let obj2 = { x: 10 };

obj1 == obj2    // false (different references)

let obj3 = obj1;
obj1 == obj3    // true (same reference)
   

Syntax: 

a==b

Note: The equality comparison is symmetric in the sense that a==b is the same as b==a.

[Example 1]: We will use the equality operator on the same data types.

JavaScript
let a = 1;
let b = 1;
let c = new String("Hello");
let d = new String("Hello");
let e = "Hello";

console.log(a==b);
console.log(c==d);
console.log(c==e);
  • c, d, e are strings, but comparison differs.
  • Strings made with new String() are objects.
  • c vs d: reference comparison - false.
  • c vs e: type conversion to string value-true.

[Example 2]: We will use the equality operator on different data types.

JavaScript
let a = 1;
let b = "1";
let c = true

console.log(a==b);
console.log(b==c);
console.log(a==c);
  • The boolean and string values are converted to numbers then comparison takes place so true is returned in all cases

[Example 3]: We will compare Date with String using the equality operator.

JavaScript
let a = new Date();
let b = a.toString();
console.log(a==b);
  • When the date object is compared with the string it is first converted to a string so the comparison returns true.
Comment

Explore