There are many way to detecting properties an Object in JavaScript, Such as: in and typeof operator, Comparing with undefined and hasOwnProperty() method. In this section we check exists properties by using in operator and hasOwnProperty() method.
Check javascript properties
Because in JavaScript properties can be added or delete at any time, it’s sometimes necessary to check whether a property exists in the object or not.
There are following way to detecting properties an Object in JavaScript:
in operator.
hasOwnProperty() method.
# using in operator
With the help of the "in" operator, you can check whether the property exists in the object.
Syntax :
'propertyName' in objectName;
Let's create an simple example :
Example 1 : in operator;
In this code, we creates "book" object and uses the "in operator" to check if the "title" properties exist in the object.
- <script type="text/javascript">
var book = {
title : "JavaScript",
};
/* Here, checking a property exists or not */
console.log('id' in book); // Output : false
console.log('title' in book); // Output : true
- </script>
# using hasOwnProperty() method
The JavaScript object has a special hasOwnProperty () method to check whether it has its own property or is a global property of all JS objects.
Note : It returns a boolean value, true or false, that indicates whether object has a property or not.
Syntax :
object_name.hasOwnProperty('prop_name');
Let's create an simple example :
Example 2 : hasOwnProperty;
In this code, the hasOwnProperty() determines the presence of properties.
- <script type="text/javascript">
var book = {
title : "JavaScript",
};
/* Here, checking a property exists or not */
book.hasOwnProperty('title'); // return : true
book.hasOwnProperty('id'); // return : false
- </script>
Explanation :
Example 2: explanation
First, define the book object that contains one properties: title.
The property 'title' exists in the object "book": thus book.hasOwnProperty('title') returns true.
On the other side, 'id' property doesn’t exist in the object "book". As expected, book.hasOwnProperty('id') returns false.
In above example
After that, check 'title' property exists or note:
# Different between in and hasOwnProperty() for detecting properties
# |
Method/operator |
Description |
---|---|---|
1 |
in operator |
The in operator/keyword checks for both 'own properties' and 'prototype properties'. |
2 |
hasOwnProperty() method |
The hasOwnProperty() method checks only own properties. |
Let's try to create an simple example :
Example 3 : check a global property;
In this code, both hasOwnProperty() and in operator determines the presence of global properties.
- <script type="text/javascript">
var book = {
title : "JavaScript",
};
'title' in book; // return : true
book.hasOwnProperty('title'); // return : true
'toString' in book; // return : true
book.hasOwnProperty('toString'); // return : false
- </script>
Explanation :
Example 3: explanation
So both the in operator and hasOwnProperty() method return true.
The in operator returns true 'for toString()' method, but hasOwnProperty() returns false.
In above example, title is an own property of book.
The toString() method, However is a prototype property that is present on all JavaScript objects().
Comments