Equality Operators (== and !=) and Identity Operators (=== and !==)

Navigation Aids -- This Page | Navigation Aids -- This Topic | Navigation Aids -- This Site | ||
---|---|---|---|---|

- equality operators
- identity operators
- any operand
- precedence
- associativity
- binary operator
- NaN
- undefined
- Equality Operator Group:The ECMAScript Language Specification section 11.9 groups both equality operators and identity operators.
- Synonyms:
- Comparison Operators: includes both the equality and identity operators. Comparison operators also include (<, >, <=, >=, etc.) but for this page, we are interested in only the equality and identity operators.

In addition to providing the basics of JavaScript Equality Operator Group, this page will show the special cases where this group of operators will perform type conversions on one or more of its operands. Some special comparison cases are explained. And finally, we explain how equality and identity operators differ.

- The Equality Operator Group are binary operators and can support an operand of any type.
- In general, equality operators work best on primitive types. The identity operators deal best with objects.
- Primitive data types are compared by value, whereas, objects, arrays and functions are compared by reference.
- Comparing two variables with the Equality Operator Group will always return a boolean result.
- The equality and identity not equal operators (!= and !==) will return true when the operands are not equal and false when the operands are equal.
- The equality and identity equal operators (== and ===) will return true when the operands are equal and false when the operands are not equal.
- Identity operators have a stricter test for equality. The data types of both operands must also be the same (in addition to data values). Operands with unequal data types will return false for the identity equal (===) operator.
- The equality operators convert all characters in a string to there ASCII equivalent before the comparison operation.
- String comparisons are case sensitive. With the ASCII character conversion, all uppercase letters are less than there equivalent lowercase letter.
- Strings of different lengths can not be equal.
- String comparisons begin on the left. The first letter of the first operand is compared to the first letter of the second operand. The operation proceeds letter by letter to the right.
- With equality operators, if two operands are of the same type, the comparison will behave like the identity operators.

Identity operators will not perform type conversions. The basic rules for type conversions prior to equality or inequality comparisons are:

- The equality operators will attempt to convert a string to a number, when the other operand is a number.
- The equality operators will attempt to convert an object to a string (use the toString() method), when the other operand is a string.
- The equality operators will attempt to convert an object to a number, when the other operand is a number.
- The equality operators will convert a Boolean true and false to numbers 1 or 0 respectively.
- null values and undefined values will not receive any type conversion regardless of the type of the other operand.

Special comparison cases:

- null values and undefined values will have an equal (true) comparison.
- If either or both operands have a NaN value, the equal operator returns false. The not equal operator returns true.
- When both operands are objects, then the reference values are compared. The equal operator returns true, when both references point to the same object.
- Two separate arrays are never equal or identical even if there elements are the same.
- Two separate objects are never equal or identical even if there properties and methods are the same.

Expression | Result | Comment |
---|---|---|

10 == 10 | true | The equal operator will return a boolean of true. |

4 == 4.0 | true | The equal operator will return a boolean of true. |

7 != 6 | true | The not equal operator will return a boolean of true. |

"Sam" != "Mike" | true | The not equal operator will return a boolean of true. |

"Kay" == "kay" | false | The equal operator will return a boolean of false. |

true == 1 | true | A boolean of true will convert to "1". |

true == 0 | false | A boolean of true will convert to "1"; 1 does not = 0. |

false == 0 | true | A boolean of false will convert to "0". |

null == null | true | Returns true; values of null are equal. |

undefined == null | true | Returns true; values of null and undefined are equal. |

undefined == undefined | true | Returns true. |

NaN == 0 | false | If either operand is NaN, the equality operator returns false. |

NaN == NaN | false | NaN can not equal NaN. |

NaN != NaN | true | Returns true. |

"22" == 22 | true | Returns true; the string is converted to a number. |

"22" === 22 | false | Returns false; tricky!! See the next topic below on identity operators. |

"22" !== 22 | true | Returns true; tricky!! See the next topic below on identity operators. |

Identify (=== and !==) operators are the same as equality operators, except, they do not convert operand types before testing for equality. In the above example, where ("22" === 22), the string will not convert to a number. Values of different types can not be equal with the identify equal operator.