When to use Double Equals and when to use Triple Equals in JavaScript: == vs. ===

Ben Tagtow
3 min readOct 24, 2020

Often times, developers that don’t have a ton of experience use == and ===, two equality operators, interchangeably in JavaScript. This is bad practice and can lead to (often hard to debug) issues with your code. Both of these operators return booleans (true or false), but knowing the differences between both the purpose and functionality of the two operators is a foundational piece of being a JavaScript developer.

Double Equals (==)

The double equals is actually just called the “equality operator” (the triple equals, ===, is the “strict equality operator.” Notice the difference in naming, this operator is not called “strict.” The reason for this is that, simply, this operator is more lenient in what can return true.

The double equals uses something called the Abstract Equality Comparison Algorithm to determine its return value. This basics of the algorithm goes as follows, but you can get a full picture of the algorithm’s rules here:

1.When operands of two different types are compared, attempt to convert these two operands to the same type before comparing (more on this below)

  • When one object is a boolean with a value true, the equality operator will convert this value to 1 before comparing. For a quick example, go into your console and create two variables, x = 1 and y = true. Then use the equality operator to compare x == y , it will return true.
  • If you’re comparing a string and a number, the operator attempts to convert the string to a number before comparing (i.e. "5" == 5 returns true)

2. When comparing two objects, only return true if they reference the same object.

3. If one value is null and the other is undefined, return true

4. When comparing two strings, return true only when both operands have the same characters in exactly the same order

5. When comparing two numbers, compare the value of those numbers. If either is not a number, return false (+0 == -0 returns true)

6. When comparing booleans, return true if both are true or if both are false

Triple Equals / Strict Equality (===)

The triple equals is called the strict equality operator. The strict equality operator, briefly , only returns true when both operands are the exact same value. Here’s a few rules that summarize the essential functionality/strictness:

1. The strict equality operator does not use type comparison, so when you are comparing, say, a string and a number ("5" === 5), regardless of either value, the return value will be false

2. If the value of both are null, return true. Ditto for both being undefined.

3. Comparing +0 === -0 still returns true, since the value of each operand is the same.

4. Comparing two values where either is NaN, the return value will be false.

Summary

Both of these operators will often give you the same return value. However, in my opinion, using the strict equality operator is almost always a better move. It minimizes potential bugs from the type comparison (and if you really need type comparison, you can create a function for that before using the operator).

Two resources I found particularly useful for creating this post were the MDN docs on the two operators: Double equals and triple equals.

--

--