Main Page

Identically equal and not identically equal

The operators also follow these rules when making comparisons:
?
Values of
null
and
undefined
are equal.
?
Values of
null
and
undefined
cannot be converted into any other values for equality checking.
?
If either operand is
NaN
, the equal operator returns
false
and the not equal operator returns
true
. Important note: Even if both operands are
NaN
, the equal operator returns
false
because,
by rule,
NaN
is not equal to
NaN
.
?
If both operands are objects, then the reference values are compared. If both operands point to
the same object, then the equal operator returns
true
. Otherwise, the two are not equal.
The following table lists some special cases and their results:
Expression
Value
null == undefined
true
“NaN” == NaN
false
5 == NaN
false
NaN == NaN
false
NaN != NaN
true
false == 0
true
true == 1
true
true == 2
false
undefined == 0
false
null == 0
false
“5” == 5
true
Identically equal and not identically equal
The brothers of the equal and not equal operators are the identically equal and not identically equal
operators. These two operators do the same thing as equal and not equal, except that they do not convert
operands before testing for equality. The identically equal operator is represented by three equal signs
(
===
) and only returns
true
if the operands are equal without conversion. For example:
var sNum = “55”;
var iNum = 55;
alert(sNum == iNum); //outputs “true”
alert(sNum === iNum); //outputs “false”
In this code, the first alert uses the equal operator to compare the string
“55”
and the number
55
and
outputs
“true”
. As mentioned previously, this happens because the string
“55”
is converted to the
number
55
and then compared with the other number
55
. The second alert uses the identically equal
51
ECMAScript Basics
05_579088 ch02.qxd 3/28/05 11:35 AM Page 51


JavaScript EditorFree JavaScript Editor     Ajax Editor


©