How to Check if an Object has a Specific Property in JavaScript

There are several methods of checking whether the object has a property or not in JavaScript. Let’s quickly explore them and find the fastest one.

The typeof Function

Most of the developers use the typeof method to check if the type of the property is undefined or not. If the object has the property with the undefined value, typeof is not recommended.

let myObj = {
  welcome: "Welcome"
};
if ("undefined" === typeof (myObj["welcome"])) {
  // The property DOESN'T exists
  console.log(undefined);
} else {
  // The property exists
  console.log(myObj["welcome"]);
}
let myObj = {
  welcome: "Welcome"
};
if ("undefined" === typeof (myObj.welcome)) {
  // The property DOESN'T exists
  console.log(undefined);
} else {
  // The property exists
  console.log(myObj.welcome);
}

The hasOwnProperty Method

Javascript object provides the hasOwnProperty native method. The method returns a boolean indicating if the object has the specified property as a first parameter.

The hasOwnProperty method does not check down the prototype chain of the object.

let myObj = {
  welcome: "Welcome to W3Docs."
};
let prop = myObj.hasOwnProperty("welcome");
if (prop) {
  // myObj has the welcome property 
  console.log(prop);
} else {
  // myObj doesn't have welcome property
  console.log(prop);
}
// False
let hasPropertyWelcome = myObj.hasOwnProperty("Javascript");
// Use hasOwnProperty in arrays too using the index
console.log(["welcome"].hasOwnProperty(0)); // true
// But no the value
console.log(["welcome"].hasOwnProperty("welcome")); // false

The in Operator

The in operator returns true if it finds the specified property in the object.

The in operator can be used both in objects and arrays.
let myObj = {
  welcome: "Welcome to W3Docs"
};
let prop = "welcome" in myObj
if (prop) {
  // Welcome property exists
  console.log(prop);
} else {
  // Welcome property exists
  console.log(prop);
}
// False
let isInObject = ("hi" in myObj);
// Use in in arrays too using the index
console.log((0 in ["welcome"])); // true
// But not the value
console.log(("welcome" in ["welcome"])); // false

To understand which method is faster, you should work with huge objects. Tests show that typeof is much faster compared to the hasOwnProperty and in methods.

The typeof Operator

The typeof operator helps to get the data type of its operand. The operand can be a literal or a data structure (an object, a function, or a variable). The typeof function returns a string with the name of the type of the variable as a first parameter (object, boolean, undefined, etc.).

hasOwnProperty vs in

The hasOwnProperty method returns a boolean, which shows whether the object contains the specified property or not.

This method determines whether the object has the specified property as a direct property of that object. Unlike the in operator, hasOwnProperty does not check for a property in the object's prototype chain. If an object is an Array, this method can check whether an index exists. If the specified property exists in the object or its prototype chain, the in operator returns true.


Do you find this helpful?

Related articles