
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
130
|
Chapter 5: Operators
3. If one operand is a number and the other is a string, convert the string to a num-
ber and go back to Step 1.
4. If either operand is a Boolean,
null,orundefined, convert the operand to a num-
ber and go back to Step 1.
5. If either operand is an object, invoke its valueOf() method to convert the object
to a primitive value and go back to Step 1. If the valueOf() method fails or does
not return a primitive value, return
false.
6. For any other condition, return
false.
Note that type conversions performed during a comparison do not alter the original
item’s stored value or datatype. The results of the temporary conversions are dis-
carded once the expression has been evaluated.
Here is a simple conversion example comparing two Booleans:
false < true // true: 0 is less than 1
Comparison operators always convert composite datatypes to strings or numbers for
comparison. In the following example, because both
someObj and someOtherObj are
members of the Object class, their string value, “[object Object]”, is the same:
someObj = new Object();
someOtherObj = new Object();
someObj <= someOtherObj; // true!
In contrast, the expression someObj = = someOtherObj yields false because objects are
equality-tested by reference, and they are therefore not converted to primitives dur- ...