Home javascript What is the difference between == and ===?

What is the difference between == and ===?

Author

Date

Category

It seems to me that the operators == and === work equally. For example:

1 == 1 // True
1 === 1 // True
1 == 2 // False
1 === 2 // False

The == operator in short and is often found in other programming languages. So why === in general?


Answer 1, Authority 100%

=== and ! == are operators strict comparisons. It means that the operands have different types, they are not equal. For example:

1 === "1" // Returns False
1! == "1" // Returns True
null === undefined // Returns False

Operators == and ! = are operators, say, rude comparisons. That is, if the operands have different types, JavaScript tries to somehow convert them to become comparable. For example:

1 == "1" // Returns True
1! = "1" // Returns False
null == undefined // Returns True

It is interesting to notice that in contrast to === , the operator == is not transitive :

"0" == 0 // True
0 == "" // True
"0" == "" // False

It is not very easy to remember the rules of this coarse comparison, and sometimes it happens that it works as an unexpected way. Therefore, I recommend using === instead of == .

Even I do not remember the small parts of the operator == , so let’s see in Specifications, Paragraph 11.9.3 :

Abstract Equality Comparison Algorithm

Comparison x == y, where x and y are values, returns True or False. This comparison is made as follows:

  1. if Type (x) is the same as Type (Y), then
    1. If Type (X) – undefined, return True.
    2. if Type (x) is NULL, return True.
    3. if Type (x) – Number, then
      1. If X is Nan, return False.
      2. If Y is Nan, return False.
      3. if X has the same numeric value as Y, return True.
      4. if x is +0 and y equal to -0, return TRUE.
      5. if x is -0 and y equal to +0, return TRUE.
      6. Return False.
    4. If type (x) is String, then return True if X and Y have an absolutely identical sequence of characters (the same length and with the same symbols in the appropriate positions). Otherwise, return False.
    5. if Type (x) is Boolean, return True if X and Y are both are or true, or false. Otherwise, return False.
    6. Return True if X and Y are referring to the same object. Otherwise, return False.
  2. If x is Null and Y – undefined, return True.
  3. if X is undefined and y – null, return True.
  4. If Type (X) – Number and Type (Y) – String,
    Return the result of the comparison X == TONUMBER (Y).
  5. if Type (X) is String and Type (Y) – Number,
    Return the result of the comparison Tonumber (x) == y.
  6. If Type (x) is Boolean, return the result of the Tonumber (x) == y comparison result.
  7. If Type (Y) is Boolean, return the result of the comparison X == Tonumber (Y).
  8. If Type (X) is either string, or Number, and Type (Y) – Object,
    Return the result of the comparison X == Toprimitive (Y).
  9. if Type (X) – Object and Type (Y) – String, or Number,
    Return the result of the comparison ToprimiTive (x) == y.
  10. Return False.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions