Learn =, == and === in Javascript

In programming, especially in JavaScript, =, ==, and === are different operators used for assignment and comparison, with distinct functionalities.

1. = (Assignment Operator)

The single = is an assignment operator. It assigns the value on the right to the variable on the left.

let value = 10;
value; // 10
console.log(value); // 10

2. == (Equality Operator)

The double == is an equality operator. It compares two values for equality after type coercion. If the types are different, JavaScript tries to convert them to the same type before comparison.

console.log(5 == '5'); // true
console.log(null == undefined); // true
console.log(0 == false); // true

3. === (Strict Equality Operator)

The triple === is a strict equality operator. It checks for both value and type equality without type coercion. If the types are different, it returns false immediately.

console.log(5 === '5'); // false
console.log('5' == 5);  // true
console.log(null === undefined); // false
console.log(0 === false); // false

Summary

  1. = is for assignment.
  2. == checks for equality with type coercion.
  3. === checks for strict equality without type coercion (value and type must both match).
Operator Name Checks Type Conversion Example (5 and "5")
= Assignment N/A N/A let x = 5;
== Equality Value Yes 5 == "5" → true
=== Strict Equality Value and Type No 5 === "5" → false

In JavaScript, it’s generally safer to use === for comparisons to avoid unexpected results due to type coercion.

Use == only when you specifically want type conversion to occur.


Similar Articles