// If o has a property named "x", then set it
if ("x" in o) o.x = 1;
The in operator is not often needed, however, because if you query a property that does not exist, the undefined value is returned. Thus, the above code can usually be written like this:
// If the property x exists and is not undefined, set it.
if (o.x !== undefined) o.x = 1;
Note that it is possible for a property to exist but still be undefined. For example, if you write:
o.x = undefined
the property x exists but has no value.
// If the doSomething property exists and is not null or undefined,
// then assume it is a function and invoke it!
if (o.doSomething) o.doSomething();