- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
In real life there's often a simple, obvious distinction between identity and equality. For instance, if you hand me two oranges I might think of them as equal, but I certainly wouldn't think of them as identical. They're equal in the sense that they have equal value to me, they taste the same, etc. They're not precisely the same, however — one might have a blotch over there, while perhaps the other has a bump over here. On the other hand, if you gave me a couple of freshly-minted dimes, right off the same stamping press — I'd think of them as not only equal, but identical in every respect.
JavaScript can make the same sort of distinction in it's values. Consider this code:
var x = 5;
var y = '5';
if (x == y)
gs.log("They're the same!");
Here 'x' and 'y' are equal (and the comparison proves that), but they are not identical. How can you tell the difference in JavaScript?
The "identity operator" ('===') is the key. If we extend our code slightly, we can tell the difference easily:
var x = 5;
var y = '5';
if (x == y)
gs.log("They're the same!");
if (x === y)
gs.log("They're identical!");
Run this, and it will log that 'x' and 'y' are the same, but not that they're identical. If you substitute a numeric 5 for the string '5' that's initializing 'y', it will log that they are both equal and identical. What exactly is going on here?
The equality operator ('==') tests whether the value on the left is equal to the value on the right, without regard to the type of the value. In the example above, the numeric value in 'x' is equal to the string value in 'y' because the numeric value is coerced to a string value before the comparison is made. Their value, in JavaScript terms, is the same — so, to JavaScript, they are equal.
The identity operator ('===') tests whether both the value and the type on the left are equal to those on the right. A numeric 5 is not identical to a string 5 in JavaScript — while the values are the same (after implicit coercion), the types are not (number vs. string). To JavaScript, these values are equal but not identical.
Probably the most common use for the identity operator is to distinguish between a null value and an undefined value. Run this code:
var x = undefined;
if (x == null)
gs.log("X is equal to null");
if (x === null)
gs.log("X is identical to null");
If your code needs to treat an undefined value differently than a null value, this is the easiest way to determine the difference.
I've found the identity operator to be quite handy a few times when it actually mattered what type a value was. Consider this function:
function log(x) {
if (x)
gs.log('Got here');
else if (!x)
gs.log('Was here');
else
gs.log(x);
}
log(true);
log(false);
log('true');
log('false');
It doesn't give the result I wanted. But this does:
function log(x) {
if (x === true)
gs.log('Got here');
else if (x === false)
gs.log('Was here');
else
gs.log(x);
}
log(true);
log(false);
log('true');
log('false');
Add the identity operator to your geekly toolbox!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.