Archive for October 10th, 2011

Operator “===” / identity operator / Triple equal operator

October 10, 2011

What is Triple equal operator? Where, Why & When it’s used?

In general,

Double equal (==) is called equality operator

Tripe equal (===) is called identity operator

Here are few gotchas, you would love reading!

Where it’s used?

Seems that in PHP, RUBY (I haven’t played with them, but got to know through googling :)) & JavaScript

Why & When it’s used?

When we have a situation to check “both operands have the same value and are of the same type”.

Let’s see some examples in JavaScript:

‘1’ === ‘1’   // true – Same value and type

‘1’ === 1    // false – Same value but different type

‘1’ === true // false – Value and type are different

Equality operators also have their opposites 🙂

The non-identity operator ( !== ) returns true, where identity would return false.

‘1’ !== ‘1’   // false

‘1’ !==  1    // true

Then, is anything there in C#?

I don’t see the exact, but we have one, that helps to check for the same “instance” type, that’s it!

C# static method ReferenceEquals(obj1, obj2), helps to check same instance or reference equality.

Reference:

http://www.color-of-code.de/index.php/articles/programming5/c-identity-vs-equality

Other useful references:

http://en.wikipedia.org/wiki/Equals_sign

http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use