{"id":1406,"date":"2024-01-16T14:53:03","date_gmt":"2024-01-16T14:53:03","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=1406"},"modified":"2024-01-17T11:03:06","modified_gmt":"2024-01-17T11:03:06","slug":"javascript-hasownproperty-method","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/javascript-hasownproperty-method\/","title":{"rendered":"JavaScript hasOwnProperty() Explained (with Examples)"},"content":{"rendered":"\n<p>In this article, we will be taking a deep dive into the hasOwnProperty() method that helps in checking whether a property exists within an object. Let us get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the hasOwnProperty() Method in JavaScript?<\/strong><\/h2>\n\n\n\n<p>We will begin with a short introduction of Objects.\u00a0 Objects are non-primitive data types in Javascript that store an unordered collection of key-value pairs. The association between the key and value is called property, so objects are a collection of properties.<\/p>\n\n\n\n<p>For example, we have defined an object \u201cstudent\u201d with various properties below:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-10a8bff48a70526aad55831f22184331\" style=\"background-color:#fedcba\"><code>let student={\n    name:'Komal',\n    rollNumber:200101,\n    age:21\n}\n<\/code><\/pre>\n\n\n\n<p>Several properties such as name, rollNumber, and age are defined for the object student\u201d. The name, rollNumber, and age here indicate the keys, and \u201cKomal\u201d, 200101 and 21 are the values corresponding to the related keys.<\/p>\n\n\n\n<p><strong>The hasOwnProperty() is an in-built method in JavaScript is used to check if an object contains a property. This method takes an argument in the string form as the parameter and checks whether that string (the property) is present in the object or not.<\/strong><\/p>\n\n\n\n<p> It returns a boolean value. If it returns true, it means that the property exists in the object. If it returns false, it means that the object doesn\u2019t contain that particular property.<\/p>\n\n\n\n<p>The<strong> <\/strong>syntax for using this method is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-a47965bbd9985194b40ade40b2018f00\" style=\"background-color:#fedcba\"><code>object_name.hasOwnProperty(\u2018property_name)<\/code><\/pre>\n\n\n\n<p>Let us understand it with the help of the previous example. We have an object \u2018employee\u2019 with certain properties(key-value pairs). Now, We will check if the object employee contains a certain property or not using this method. The code for the same is given below:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/defining an object employee\nlet employee={\n    empId:2301,\n    name: &quot;Joshua&quot;,\n    email:&quot;josh232@gmail.com&quot;,\n    age:32\n};\n\n\/\/both the statements below return true\nconsole.log(employee.hasOwnProperty('name'));\nconsole.log(employee.hasOwnProperty('age'));\n\n\/\/below statement returns false\nconsole.log(employee.hasOwnProperty('salary'));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-deb50932b6886ba2dd2c1082c5222403\" style=\"background-color:#fedcba\"><code>true\ntrue\nfalse\n<\/code><\/pre>\n\n\n\n<p>The code above uses the inbuilt method \u2018hasOwnProperty(\u2018property_name\u2019)\u2019 and returns true for the properties &#8211; name and age. It returns false when checking for \u201csalary\u201d.<\/p>\n\n\n\n<p>When working with objects in JavaScript, it&#8217;s important to distinguish between properties that are directly defined on the object and those inherited from its prototype. <strong>Using hasOwnProperty allows developers to ensure they are only considering properties that belong directly to the object.<\/strong><\/p>\n\n\n\n<p>The hasOwnProperty() method also considers properties with values of null or undefined as direct properties of the object. Let us see an example of the sample:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/defining an object employee\nlet employee={\n    empId: null,\n    name: undefined,\n};\n\n\/\/both the statements below return true\nconsole.log(employee.hasOwnProperty(&quot;empId&quot;));\nconsole.log(employee.hasOwnProperty(&quot;name&quot;));<\/pre><\/div>\n\n\n\n<p>In this example, we have an object employee with properties empId and name, both having values of null and undefined, respectively. Themethod returns true for both properties, indicating that they are direct properties of the object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>hasOwnProperty() vs the &#8220;in&#8221; Operator<\/strong><\/h2>\n\n\n\n<p><strong>While the hasOwnProperty() method is effective for checking the direct properties of an object, the &#8220;in&#8221; operator is used to determine if a property exists in an object, regardless of whether it is a direct property or an inherited property.<\/strong><\/p>\n\n\n\n<p>The syntax of the in operator is:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-48f7917c3d7fe1714d27297dc790d4cd\" style=\"background-color:#fedcba\"><code>\u201cpropertyName\u201d in objectName;<\/code><\/pre>\n\n\n\n<p>where,&nbsp;<\/p>\n\n\n\n<p><strong>propertyName:<\/strong> name of the property to check<\/p>\n\n\n\n<p><strong>objectName:<\/strong> name of the object in which property has to be checked<\/p>\n\n\n\n<p>Let us see an example of the same:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/defining an object employee\nlet employee={\n    empId:2301,\n    name: &quot;Joshua&quot;,\n    email:&quot;josh232@gmail.com&quot;,\n    age:32\n};\n\n\/\/both the statements below return true\n'name' in employee;\n'age' in employee;\n\n\/\/below statement returns false\n'salary' in employee<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-deb50932b6886ba2dd2c1082c5222403\" style=\"background-color:#fedcba\"><code>true\ntrue\nfalse\n<\/code><\/pre>\n\n\n\n<p>The above code checks if the name and age properties exist in the object \u2018employee\u2019 and hence returns true, whereas returns false when checking for the property \u2018salary\u2019 as it doesn\u2019t exist in the object.<\/p>\n\n\n\n<p>It&#8217;s important to choose the appropriate method based on the specific use case.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If we want to check for direct properties only, use the hasOwnProperty() method.&nbsp;<\/li>\n\n\n\n<li>If we need to check for properties in both direct and inherited ways, the &#8220;in&#8221; operator is the preferred choice.<\/li>\n<\/ul>\n\n\n\n<p><strong>Can hasOwnProperty be used with any type of object?<\/strong> Yes, this method available on all objects in JavaScript. It can be used with objects created using object literals, constructor functions, or instances of built-in classes.<\/p>\n\n\n\n<p>However, one of the common limitations when iterating over object properties, leading to unintended inclusion of inherited properties. It&#8217;s essential to keep this in mind to ensure accurate and expected behavior when working with objects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion&nbsp;<\/strong><\/h2>\n\n\n\n<p>In this article, we discussed the objects and the hasOwnProperty() method to <a href=\"https:\/\/favtutor.com\/articles\/check-if-object-has-property-javascript\/\">check if a property exists within an object<\/a>. Then we compared the hasOwnProperty() method and the \u201cin\u201d operator. Both methods are used to check the existence of a property within an object but have certain differences.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn everything you need to know about hasOwnProperty() Method in JavaScript for beginners with Examples.<\/p>\n","protected":false},"author":9,"featured_media":1409,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11],"class_list":["post-1406","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=1406"}],"version-history":[{"count":5,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1406\/revisions"}],"predecessor-version":[{"id":1426,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1406\/revisions\/1426"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1409"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=1406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=1406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=1406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}