Detect An Undefined Object Property In JavaScript




Here we consider how to detect an undefined object in javascript. Lets start with one exmaple here. Suppose we have an object in javascript as shown here. It has three properties with their values.

var myObject = {
    name: "Alex",
    age: 30,
    email: "myemail@email.com"
};



Now we wants to check whether an object property named sex is undefined or not?. This can be done by checking the property's type. If the type is undefined, we can say the property named sex is undefined.

if (typeof myObject.sex === "undefined") {
     alert("sex property is undefined");
}



By this way we can detect an undefined object in javascript.