Checking If an Object Has a Property in Javascript

Checking if an object has a property in JavaScript means determining whether or not a particular key exists within an object, and potentially accessing its associated value. This process involves using specific methods or operators to reliably verify the presence of a property.  This is often necessary in programming to avoid errors when trying to access or manipulate properties that may not be defined.

Here are the primary ways to check for a property in JavaScript:

Using hasOwnProperty method:
  1. The hasOwnProperty method is a built-in method of the Object prototype in JavaScript.
  2. Checks for direct properties only: It determines if a property is directly owned by the object itself, not inherited from its prototype chain.
  3. Syntax: object.hasOwnProperty(propertyName)
  4. Returns: true if the property exists directly on the object, false  otherwise.

 
const school = { name: "Central High", address: "123 Main St" };
const teacher = { name: "Ms. Smith", subject: "Math" };
 
console.log(school.hasOwnProperty("name")); 
 // true
console.log(school.hasOwnProperty("address"));
// true
console.log(school.hasOwnProperty("teachers"));
// false (not a direct property)
 
console.log(teacher.hasOwnProperty("toString"));
// false (inherited from Object.prototype)
 
 
 

Using in operator:
  1. The in operator is used to check if an object has a specified property, including properties inherited from its prototype chain.
  2. It returns true if the property exists, whether it's in the object itself or in its prototype chain; otherwise, it returns false.

 
const Animals = {
    dog: "Canis lupus familiaris",
    cat: "Felis catus",
    horse: "Equus ferus caballus",
};
// Check for "dog" property:
console.log("dog" in Animals);   // Output: true
 
// Check for "bear" property (not present):
console.log("bear" in Animals);   // Output: false
 
// Check for "toString" property (inherited from Object prototype):
console.log("toString" in Animals);   // Output: true
 
if ("lion" in Animals) {
console.log("The scientific name of lion is:", Animals.lion);
} else {
console.log("The Animals object doesn't have information about lions.");
}

Example with Prototype Chain:

 
const Company = function() {};  // Empty constructor
Company.prototype.industry = "Technology";  // Assign property to prototype
 
const company = new Company();
company.name = "Google";
 
console.log(company.hasOwnProperty("industry")); 
// false (correctly indicates inheritance)
console.log("industry" in company);       
    // true (property still accessible via prototype)
 

The in operator is used to check if a property exists anywhere in the prototype chain of the company object. It returns true if the property is found, regardless of whether it is defined directly on the object or inherited from its prototype. In this case, "industry" exists in the prototype chain of the company object, so the result is true.

 
// Prototype chain example
function Animal(type) {
  this.type = type;
}
 
function Dog(name) {
  this.name = name;
}
 
Dog.prototype = new Animal("Mammal");
 
let myDog = new Dog("Buddy");
 
console.log("name" in myDog);  // true
console.log("type" in myDog);  // true
 

In this example, myDog is an instance of the Dog constructor, and it inherits properties from the Animal constructor through the prototype chain. The in operator can be used to check both the own properties and those inherited from the prototype chain.