JavaScript – Identify whether a property is defined and set to 'undefined', or undefined-Collection of common programming errors

object.hasOwnProperty(name) only returns true for objects that are in the same object, and false for everything else, including properties in the prototype.

function x() {
  this.another=undefined;
};

x.prototype.something=1;
x.prototype.nothing=undefined;
y = new x;

y.hasOwnProperty("something"); //false
y.hasOwnProperty("nothing"); //false
y.hasOwnProperty("another"); //true

"someting" in y; //true
"another" in y; //true

Additionally the only way do delete a property is to use delete. Setting it to undefined do NOT delete it.

The proper way to do it is to use in like roborg said.

Update: undefined is a primitive value, see ECMAScript Language Specification section 4.3.2 and 4.3.9.

Originally posted 2013-11-09 22:49:05.