Tuesday, June 21, 2016

javascript - Detecting an undefined object property




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


Answer



Use:



if (typeof something === "undefined") {
alert("something is undefined");
}


If an object variable which have some properties you can use same thing like this:




if (typeof my_obj.someproperties === "undefined"){
console.log('the property is not available...'); // print into console
}

No comments:

Post a Comment