ECMAScript Equality Operators

Determining whether two variables are equal is a very important operation in program design. When dealing with primitive values, this operation is quite simple, but when involving objects, the task becomes a bit more complex.

ECMAScript provides two sets of equality operators: the equality and non-equality operators are used to handle primitive values, and the strict equality and non-strict equality operators are used to handle objects.

Equality and Non-equality

In ECMAScript, the equality operator is represented by double equals (==), and it returns true only when both operands are equal. The non-equality operator is represented by exclamation mark plus equals (!=), and it returns true only when both operands are not equal. To determine whether two operands are equal, both operators perform type conversion.

The rules for performing type conversion are as follows:

  • If one operand is a Boolean value, it should be converted to its numeric value before checking for equality. false is converted to 0, and true to 1.
  • If one operand is a string and the other is a number, the string should be converted to a number before checking for equality.
  • If one operand is an object and the other is a string, the object should be converted to a string before checking for equality.
  • If one operand is an object and the other is a number, the object should be converted to a number before checking for equality.

When comparing, the operator also follows the following rules:

  • The values null and undefined are equal.
  • When checking for equality, null and undefined cannot be converted to other values.
  • If one of the operands is NaN, the equality operator will return false, and the non-equality operator will return true.
  • If both operands are objects, the comparison is made with their reference values. If both operands point to the same object, the equality operator returns true; otherwise, the two operands are not equal.

Important Note:Even if both numbers are NaN, the equality operator returns false because, according to the rules, NaN is not equal to NaN.

The following table lists some special cases and their results:

expression value
null == undefined true
"NaN" == NaN false
5 == NaN false
NaN == NaN false
NaN != NaN true
false == 0 true
true == 1 true
true == 2 false
undefined == 0 false
null == 0 false
"5" == 5 true

Strict Equality and Non-strict Equality

The same types of operators as the equality and non-equality operators are the strict equality and non-strict equality operators. These operators do the same as the equality and non-equality operators, but they do not perform type conversion before checking for equality.

The strict equality operator is represented by three equal signs (===), and it returns true only when the operands are equal without type conversion.

For example:

var sNum = "66";
var iNum = 66;
alert(sNum == iNum);	//Output "true"
alert(sNum === iNum);	//Output "false"

In this code, the first alert uses the equality operator to compare the string "66" and the number 66, outputting "true". As mentioned before, this is because the string "66" will be converted to the number 66, and then compared with another number 66. The second alert uses the strict equality operator to compare a string and a number without type conversion, of course, the string is not equal to the number, so the output is "false".

The non-strict inequality operator is represented by an exclamation mark followed by two equal signs (!==), and it returns true only when the operands are not equal without type conversion.

For example:

var sNum = "66";
var iNum = 66;
alert(sNum != iNum);	//Output "false"
alert(sNum !== iNum);	//Output "true"

Here, the first alert uses the non-equal sign to convert the string "66" to the number 66, making it equal to the second operand 66. Therefore, the result is "false" because the two operands are equal. The second alert uses the non-strict equality operator. This operation is asking: "Are sNum and iNum different?" The answer to this question is: yes (true), because sNum is a string, and iNum is a number, of course they are different.