User Name:

User Email:

This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2021 - First Crazy Developer (Abhishek Kumar)

crazydeveloper Using the == versus the strict === operator

The == operator compares the values of its operands. If their values are equal, it returns true. If the operands have different data types, JavaScript attempts to convert these to an appropriate type then compares their values. Consider this:

  1. alert("1" == 1); // => true

Here the string "1" is compared to the numeric 1. JavaScript converts the string numeric literal to a number value. Since they are equal, it returns true. How about this:

  1. alert(false == "0"); // => true

This returns true. In this case, both operands are converted to numbers. Boolean true values are converted to 1 and false to 0. And the string "0" is converted to the number 0. Following the conversions JavaScript compares 0 == 0 and returns true.

Here are a few more cases:

  1. alert(null == undefined); // => true
  2. alert(true == "1"); // => true
  3. alert("9" < "P"); // => true

In the last expression, the ASCII values for numbers are lower than those for letters, returning true.

The rules of data conversion with the == operator are complex and hard to remember. This can easily lead to bugs. Fortunately JavaScript offers an alternative, called the strict equality operator (===) which does not convert data when testing for equality. It only returns true when 1) the operands being compared are of the same type and 2) their values are the same. Here are some examples:

  1. alert("1" === 1); // => false
  2. alert(true === !false); // => true

If both operands are objects and if they refer to the same object, then JavaScript returns true. Let's see how this works with, say, arrays:

  1. var ref1 = [10, 20]; // an array
  2. var ref2 = [10, 20]; // a second array
  3. alert(ref1 == ref2); // => false
  4. alert(ref1 === ref2); // => false

Here ref1 == ref2 returns false, because they reference different arrays. The strict comparison ref1 === ref2 also return false because, although they have the same values, these are two different arrays in memory.

As in many other languages, JavaScript strings are immutable meaning that the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this. Let's see how this affects == and === operations:

  1. var str1 = "Hello World"; // a string
  2. var str2 = "Hello" + " World"; // another string
  3. alert(str1 == str2); // => true
  4. alert(str1 === str2) // => true

Here str1 and str2 reference the same immutable "Hello World" string and therefore both == and === return true.

How about comparing string objects created with the String constructor and with a string literal?

  1. alert("Hello" == new String("Hello")); // => true
  2. alert("Hello" === new String("Hello")); // => false

The == operator converts these objects on either side to the same type and then returns true because their values are the same. The === operator understands that the objects are not of the same type and therefore immediately returns false without even looking at their values.

crazydeveloper Home Page 23 April 2015

Become a Fan