How to Check if an Object has a Specific Property in JavaScript ?
Last Updated :
18 Dec, 2023
In JavaScript, objects can have properties that store data or functions. Sometimes it is necessary to check whether an object has a specific property. This can be useful, for example, when you want to avoid calling a function on an object that doesn't have that function defined as a property. In this article, we will be discussing different approaches to check whether an object has a specific property or not.
These are the following approaches:
Using the "in" operator
The "in" operator checks if a property exists in an object or its prototype chain.
Syntax:
if ('propertyName' in objectName) {
// Code to execute if property exists
}
Here, "propertyName" is the name of the property you want to check, and "objectName" is the name of the object you want to check.
Example: This example shows the use of in operator.
JavaScript
const person = {
name: 'John',
age: 30,
address: {
city: 'New York',
state: 'NY'
}
};
if ('age' in person) {
console.log('Person object has the age property');
}
if ('email' in person) {
console.log('Person object has the email property');
} else {
console.log('Person object does not have the email property');
}
OutputPerson object has the age property
Person object does not have the email property
Using hasOwnProperty() method
The hasOwnProperty() method checks if an object has a property of its own and is not inherited from its prototype chain.
Syntax:
if (objectName.hasOwnProperty('propertyName')) {
// Code to execute if property exists
}
Here, "propertyName" is the name of the property you want to check, and "objectName" is the name of the object you want to check.
Example: This example shows the use of hasOwnProperty() method.
JavaScript
const person = {
name: 'John',
age: 30,
address: {
city: 'New York',
state: 'NY'
}
};
if (person.hasOwnProperty('age')) {
console.log('Person object has the age property');
}
if (person.hasOwnProperty('email')) {
console.log('Person object has the email property');
} else {
console.log('Person object does not have the email property');
}
OutputPerson object has the age property
Person object does not have the email property
Using the "typeof" Operator
You can also use the "typeof" operator to check if an object has a specific property.
Syntax:
if (typeof object.property !== 'undefined') {
// Property exists in object
} else {
// Property does not exist in object
}
Here, "property" is the name of the property that you want to check for, and "object" is the object that you want to check. The "typeof" operator returns the type of the value of the specified property, so if the property does not exist in the object, it will return "undefined".
Example: In this example, we're using the "typeof" operator to determine the type of the "myVar" variable, which is a string. You can use the "typeof" operator to check for different types of values, such as "number", "boolean", "undefined", "object", "function", and "symbol".
JavaScript
const myVar = "Hello World";
console.log(typeof myVar); // Output: string
Similar Reads
How to check a JavaScript Object is a DOM Object ? Checking if a JavaScript object is a DOM object involves verifying whether the object represents an element or component within the Document Object Model (DOM) of an HTML or XML document. This can be done by checking if the object is an instance of Node, Element, or other specific DOM interfaces.Wha
2 min read
How to Check Presence of a Specific Object Property in an Array ? Checking for the presence of a specific object property in an array involves determining whether any object within the array possesses the specified property. This task is commonly encountered when working with arrays of objects and needing to verify if a particular property exists across any of the
3 min read
How to Detect an Undefined Object Property in JavaScript ? Detecting an undefined object property is the process of determining whether an object contains a certain property, and if it does, whether the value of that property is undefined. This is an important concept in JavaScript programming, as it helps to prevent errors that can occur when attempting to
3 min read
How to Remove a Property from All Objects in an Array in JavaScript? To remove a property from all objects in an array in JavaScript, you can use the forEach() method to iterate over each object in the array and use the delete operator to remove the specified property.Example:JavaScriptconst arrayOfObjects = [ { name: "Alice", age: 25, city: "New York" }, { name: "Bo
2 min read
How to read properties of an Object in JavaScript ? Objects in JavaScript, it is the most important data type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data-types(Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data-types all store a singl
2 min read
How to get property descriptors of an object in JavaScript ? Here, we are going to discuss the property descriptors of an Object in JavaScript. The Object.getOwnPropertyDescriptor() method returns an object describing a specific property on a given object. A JavaScript object can be created in many ways and its properties can be called by using property descr
2 min read
How to check whether an object exists in javascript ? Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value. This helps avoid errors when accessing or manipulating objects that may be undefined, null, or not initialized properly.Here we have some common approaches to
3 min read
How to Check Object is an Array in JavaScript? There are two different approaches to check an object is an array or not in JavaScript.1. Using Array.isArray() MethodThe Array.isArray() method determines whether the value passed to this function is an array or not. This method returns true if the argument passed is an array else it returns false.
1 min read
How to Check a Key Exists in JavaScript Object? Here are different ways to check a key exists in an object in JavaScript.Note: Objects in JavaScript are non-primitive data types that hold an unordered collection of key-value pairs. check a key exists in JavaScript object1. Using in Operator The in operator in JavaScript checks if a key exists in
2 min read
How to Check an Object is Empty using JavaScript? These are the following ways that can be used to Check an Object is Empty using JavaScript: 1. Using Object.keys() Method - Mostly usedThe Object.keys() method returns an array that contains the property names of an object. If the length of array is 0, then object is empty.JavaScriptlet obj = {}; if
2 min read