Tuesday, 21 June 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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...