Availability: |
| ||||||||
Property/method value type: | Boolean primitive | ||||||||
JavaScript syntax: | - | anOperand1 == anOperand2 | |||||||
Argument list: | anOperand1 | A value that can reasonably be compared | |||||||
anOperand2 | A value that can reasonably be compared with anOperand1 |
The result is the Boolean value true if anOperand1 is numerically or lexically equal to anOperand2, otherwise false is returned.
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 left to right.
Refer to the operator precedence topic for details of execution order.
Refer to the Equality expression topic for a discussion on the ECMA standard definition of the equality testing rules.
Be careful not to confuse the single equals with the double equals. 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. It will also force the relational expression to yield true as well. 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.
A triple equals sign further complicates things as it is a test for identical type as well as equal value.
// Fuzzy matching of numeric values function almost_equal(aValue1, aValue2) { var myPrecision = 1e-10; if((Math.abs(aValue1 - aValue2)) < myPrecision) { return(true); } return(false); }
See also: | ASCII, Type conversion, typeof, Unicode |
ECMA 262 edition 2 - section - 11.9.1
ECMA 262 edition 3 - section - 11.9.1
Wrox Instant JavaScript - page - 36
Prev | Home | Next |
Environment | Up | Equality expression |
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. |