Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
14 lines (11 sloc) 459 Bytes

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.