How do I check if an object property in JavaScript is undefined?

Spread the love

Here are 3 ways to check if the object property in Javascript to check undefined or not.

if(o.test=== undefined) {
alert(“you test property has 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 to access it:

if(!o.hasOwnProperty(‘test’)) {
alert(“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:

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

admin

admin

Leave a Reply

Your email address will not be published. Required fields are marked *