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



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




Review of Terms Related to This Topic





The Purpose for this Page

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 Basics of the Equality Operator Group





Type conversion for Equality Compares

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





Special Comparison Cases

Special comparison cases:

Special Case Examples
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.




How Equality and Identity Operators Differ

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.



Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson