Skip to content

Challenge Testing Objects For Properties

SaintPeter edited this page · 2 revisions
Clone this wiki locally

Challenge: Testing Objects for Properties

Sometimes it is useful to check if the property of a given object exists or not. We can use the .hasOwnProperty([propname]) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not.

Example

var myObj = {
  top: "hat",
  bottom: "pants"
};
myObj.hasOwnProperty("top");    // true
myObj.hasOwnProperty("middle"); 
Something went wrong with that request. Please try again.