operand1 === operand2
operand1 !== operand2
Use the strict equality operator (===) when you want to check that the two operands are of the same type and value. Use the regular equality operator (==) if you care only about the value and the type does not matter. If, for example, one operand is the number 5 and the other operand is the string “5”, standard equality operator will return true, but, since they are not of the same type, a strict equality operator will return false.
Comparing two operands may be tricky at times. Comparing numbers is probably the most straightforward. Numbers are equal when they are numerically equal, or to put it differently, the difference between them is zero. Strings are compared according to a standard lexicographical ordering, using Unicode values. Therefore, two strings are equal when they have the same length, same sequence of characters, exactly in the same corresponding positions. The number
NaN (see our previous column) is not equal to anything, including itself. Positive and negative zeros are equal.
Two objects are equal if they refer to the exact same
Object. In the assembly line example from our previous page, we use the following line to construct the
volvoInterior = new interior(“blue”, “leather”, true);
If we duplicate this line to construct two seemingly-identical objects,
volvoInterior1 = new interior(“blue”, “leather”, true);
volvoInterior2 = new interior("blue", "leather", true);
a strict equality operator:
if (volvoInterior1 === volvoInterior2)
false. Only when objects are identical, will the strict equality operator return
if (volvoInterior1 === volvoInterior1)
But this behavior of object comparison is valid for standard equality operator as well. The comparison:
if (volvoInterior1 == volvoInterior2)
false, while the following operation:
if (volvoInterior1 == volvoInterior1)
Comparing a string with a number highlights the difference between strict equality and standard equality operators.
if (5 === “5”)
false, while the following test:
if (5 == “5”)
Boolean operands are equal if they are both