The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
SlightlyLoony
Tera Contributor

find_real_file.pngYesterday I showed you how to represent things in a JavaScript object. But what if you have the opposite problem — you have an object, but you want to know what it represents? More precisely, you want to know if it has a particular property. Is there a way to do this in JavaScript?

Of course there is. Don't you know by now that there isn't anything that JavaScript can't do?

Suppose your writing a function raptoflivver that takes one argument, and you want to see if that argument has a property named zip. You could do something like this:


function raptoflivver(arg) {
if ('zip' in arg) {
// do stuff with zip, since we have the darned thing...
}
}

The in operator returns true or false depending on whether the given property name exists in the given object. You won't often see that in operator being used, though. There's a shortcut most JavaScript programmers use instead that is almost (but not quite!) exactly the same thing:

function raptoflivver(arg) {
if (arg.zip) {
// do stuff with zip, since we have the darned thing...
}
}

The if statement will evaluate as true if the zip property exists and the property itself evaluates to true. These two approaches differ in one important respect. The first version (using the in operator) tests only for the existence of the property. The second version tests both for the existence of the property and its value. Both of these behaviors can be useful, but both of them can lead to unexpected results if you aren't careful. Consider this test code:

var zippo = {};
zippo.zip = null;
raptoflivver(zippo);

If we ran that test against the first function definition, the function would see that the zip property exists, and it would try to do something with it. That may not be the behavior you were looking for if the value was null! On the other hand, if we ran that same code against the second function definition, then the zip property exists, but it's null (which evaluates to false) so it wouldn't try to do anything zippy.

Well, that settles it! The second version is better, right? Whoa there, big scripter! Consider this test code:

var zippo = {};
zippo.zip = false;
raptoflivver(zippo);

Now the first function will see that zip exists, and it will do stuff with it — and quite possibly that's exactly what we want it to do, as perhaps we're using zip as a boolean variable (maybe to control whether the zip code is included in an address). But the second function will see the false value and will not execute anything zippy.

Both methods have their uses. When using the in operator, you need to be aware that a property can exist (so in will return true), yet can have either a null or an undefined value assigned to it. When using the shortcut, you need to be aware that a property can exist, yet have a value that evaluates to false when coerced to a boolean.

Go forth and test some properties!