Equal to (==) (Operator/equality)

Compares two operands for equality.

Availability:

ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Netscape Enterprise Server - 2.0
Opera - 3.0
Property/method value type:Boolean primitive
JavaScript syntax:-anOperand1 == anOperand2
Argument list:anOperand1A value that can reasonably be compared
anOperand2A 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.

Warnings:

Example code:

   // 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

Cross-references:

ECMA 262 edition 2 - section - 11.9.1

ECMA 262 edition 3 - section - 11.9.1

Wrox Instant JavaScript - page - 36