Equal comparison operator vs Strict comparison operator - in JavaScript

MrunalTech Lead
CERTIFIED EXPERT
Technical enthusiastic, Sport Person, father
Published:
This article discusses the difference between strict equality operator and equality operator in JavaScript.

The Need:
Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when writing code.

The equality operator is denoted as “==” while strict equality operator is denoted as “===”.

Equality operator: (==)

This comparison operator returns true only when they are equal without comparing their types.

If two operands are of different types, JavaScript converts the operands and then applies strict comparison between them.

For example,
true == 1;     //true, because 'true' is converted to 1 and then compared.
"2" == 2;       //true, because 2 (the integer) is converted to "2" (the string) and then compared.

Strict equality operator: (===)

This === operator is exists in JavaScript because JavaScript is weakly typed language. For other languages, it is not needed for type comparison because other languages already have reserved words available to perform those functions. But for JavaScript, these special comparison operators make more sense.
 
You can use this operator only when exact type of operand is important for comparison.

For example,
true === 1;   //false
"2" === 2;     // false

What JavaScript does for comparison?

When type conversion is needed, JavaScript converts String, Number, Boolean or Object operands as follows:

1. When comparing a number and a string, the string is converted to a number value. JavaScript attempts to convert the string numeric literal to a Number type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest Number type value.
2. If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.
3. If an object is compared with a number or string, JavaScript attempts to return the default value for the object. Operators attempt to convert the object to a primitive value, a String or Number value, using the valueOf and toString methods of the objects. If this attempt to convert the object fails, a run-time error is generated.
4. If both operands are objects, they're compared as objects and the equality test is true only if both refer the same object.[1]

Performance:

If we consider theoretically, because strict equality operator (===) does not do conversion for comparison is faster than == operator.

Now let us execute below programs and we will calculate the time for their execution:

var n = 0;
                      while(true) {
                          n++;
                          if(n==100000) break;
                      }

Open in new window


and

var n = 0;
                      while(true) {
                          n++;
                          if(n===100000) break;
                      }

Open in new window


Here are results: (averaged):

For the first one (i.e. for == operator): 24 ms
For the second one (i.e. for === operator): 26 ms

If you see the total time required to execute for both comparisons, for over 100000 iterations, the difference is very less.

So strict equality operator (===) should be used for type safety and code equality reason, 'performance' should not the reason to use.

Finally here are some examples:

        alert('' == '0');  // false
                              alert(0 == '');  // true
                              alert(0 == '0');  // true
                              alert(false == 'false');  // false
                              alert(false == '0');  // true
                              alert(false == undefined);  // false
                              alert(false == null);  // false
                              alert(null == undefined);  // true
                              alert(' \t\r\n ' == 0);  // true

Open in new window


For strict comparison (using === ) for above all examples, results into false.

Conclusion:
It is recommended to use strict equality operator(===), when one has to perform comparison with type check.

Good references:

1. https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators

2. http://www.ecma-international.org/publications/standards/Ecma-262.htm

3. http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use#957602
0
4,721 Views
MrunalTech Lead
CERTIFIED EXPERT
Technical enthusiastic, Sport Person, father

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.