The two operands are compared and the Boolean true value is returned if they are equal and of the same type.
The equality operator is not always transitive. For example, two string objects may represent the same string value. Comparing the objects for equality will yield false because the references to the objects are being compared and not the object values themselves. However forcing a string comparison may in fact yield a true value when testing for equality. Do this by converting the objects as part of the comparison process by type conversion or valueOf() methods.
Numeric values may require rounding to take place, and testing for equality may be accurate down to a precision finer than your script cares about. You can set a precision value in a variable, then subtract the value you are comparing with from the absolute value of the comparee. If the difference is smaller than your precision value, then the two values are close enough to yield a match.
The associativity is from left to right.
Refer to the Operator Precedence topic for details of execution order.
The result is the Boolean value true if anOperand1 is numerically or lexically equal to anOperand2 and both operands are of the same type otherwise false is returned.
Be careful not to confuse the single and double equals operators with the triple equals operator.
Placing a single equals in place of a test for equality will assign the right-hand value to the left-hand operand and clobber the value accidentally. Placing a single equals sign instead of the identity operator has the same effect.
Using the equality operator in place of the identity operator is more subtle. Sometimes the test will appear to work correctly because the values in the two objects could be the same. That would have failed the identity test because they may be equal but are not identical.
The interpreter may be forgiving enough that a run-time error isn't generated, but the side effects could be subtle and make it hard to diagnose the cause.
This is not available for use server-side with Netscape Enterprise Server 3.
<HTML> <HEAD></HEAD> <BODY> <SCRIPT> myObject1 = 100; myObject2 = "100"; if(myObject1 == myObject2) { document.write("Objects are equal<BR>"); } else { document.write("Objects are NOT equal<BR>"); } if(myObject1 === myObject2) { document.write("Objects are identical<BR>"); } else { document.write("Objects are NOT identical<BR>"); } </SCRIPT> </BODY> </HTML>
ECMA 262 edition 3 - section - 11.9.4
O'Reilly JavaScript Definitive Guide - page - 48
Wrox Instant JavaScript - page - 39
Prev | Home | Next |
ID="..." | Up | Identifier |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |