Detecting an undefined object property

Asked : Nov 17

Viewed : 26 times

What's the best way of checking if an object property in JavaScript is undefined?

javascript object undefined object-property 

Nov 17

5 Answers

The usual way to check if the value of a property is the special value undefined, is:

if(o.myProperty === undefined) {
  alert("myProperty value is the special value `undefined`");
}

To check if an object does not actually have such a property, and will therefore return undefined by default when you try and access it:

if(!o.hasOwnProperty('myProperty')) {
  alert("myProperty does not exist");
}

To check if the value associated with an identifier is the special value undefined, or if that identifier has not been declared. Note: this method is the only way of referring to an undeclared (note: different from having a value of undefined) identifier without an early error:

if(typeof myVariable === 'undefined') {
  alert('myVariable is either the special value `undefined`, or it has not been declared');
}

In versions of JavaScript prior to ECMAScript 5, the property named "undefined" on the global object was writeable, and therefore a simple check foo === undefined might behave unexpectedly if it had accidentally been redefined. In modern JavaScript, the property is read-only.

However, in modern JavaScript, "undefined" is not a keyword, and so variables inside functions can be named "undefined" and shadow the global property.

If you are worried about this (unlikely) edge case, you can use the void operator to get at the special undefined value itself:

if(myVariable === void 0) {
  alert("myVariable is the special value `undefined`");
}

answered Jan 17


if ( typeof( something ) == "undefined") 

This worked for me while the others didn't.

answered Jan 17


Here we consider how to detect an undefined object in javascript. Let's start with one example 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: "[email protected]"
};

Now we want 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");
}

In this way, we can detect an undefined object in javascript.

answered Jan 17


You can use the typeof operator in combination with the strict equality operator (===) to check or detect if a JavaScript object property is undefined.

Let's take a look at the following example to understand how it actually works:

var person = {
    name: "Clark",
    age: 32,
    country: "United States"
};

// Access an existing object property
console.log(person.age); // Prints: 32

// Detect if object property is undefined
if(typeof person.gender === "undefined"){
    console.log("This property is not defined.");
}

answered Jan 17


if (typeof object.key === 'undefined') {
  // key is undefined
}

answered Jan 17


Login and Submit Your Answer
Browse other questions tagged  javascript  object  undefined  object-property or ask your own question.