0

Each of the following two items contain the same string value but the comparison using '===' returns false.

(hiddenColumns[hidenColsIndex] === cells[metricColsIndex].columnName)

This comparison using '==' returns true

(hiddenColumns[hidenColsIndex] == cells[metricColsIndex].columnName)

Why?

Craig Wilcox
  • 1,577
  • 1
  • 12
  • 23
  • 2
    If the `===` comparison is returning `false`, then they're **not** the same string value. Perhaps one is a number and the other is a string version of that number. – Pointy Jun 27 '12 at 17:49
  • We need more information about that. You might use JSON.stringify to inspect and show us those strings. – Bergi Jun 27 '12 at 17:49
  • 1
    The triple equal operator means not only must the values be equal, but their types be equal as well. Do `typeof()` on both and see what you get. – sachleen Jun 27 '12 at 17:49
  • May be of interest: http://jsfiddle.net/QjSYG/9/ – jbabey Jun 27 '12 at 17:52
  • http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use – you786 Jun 27 '12 at 17:52

3 Answers3

1

You aren't comparing two objects of the same type. One of the values (or both) is not a string.

Techgration
  • 516
  • 4
  • 16
  • You are correct indeed. While the Visual Studio debugger reports that both are of Type 'String' in the Watch Window, typeof() reveals that one is an Object and the other is a String. Good call. Here's a link to a discussion of the difference between Text Strings and String Objects [link]http://www.irt.org/articles/js028/ – Craig Wilcox Jun 28 '12 at 18:15
1
//a and b are equal in data but not equal in type
var a = 1;
var b = "1";

//== will return true
if (a == b)
{
    console.log("true");
}
else
{
    console.log("false");
};

//=== will return false
if (a === b)
{
    console.log("true");
}
else
{
    console.log("false");
};
Shiala
  • 486
  • 3
  • 8
0

=== means compare values and datatypes.

When you want to compare two elements for their value as well as their data type, === should be false.

In your case, the first statement is false because they are two different datatypes In second case, the statement is true because, the values are same ( considering two plain objects are compared)

madhairsilence
  • 3,787
  • 2
  • 35
  • 76