{"id":592,"date":"2016-11-24T07:28:00","date_gmt":"2016-11-24T07:28:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=592"},"modified":"2025-02-21T09:48:26","modified_gmt":"2025-02-21T09:48:26","slug":"properties-of-a-javascript-object","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/properties-of-a-javascript-object","title":{"rendered":"How To Print or Enumerate Properties of a JavaScript Object?"},"content":{"rendered":"\n<p>In this post, we will explore all the options to iterate and print properties of a JavaScript object. I usually come across the following requirements,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>How to print name of all the properties of an object?<\/li>\n\n\n\n<li>How to print only the methods of an object?<\/li>\n\n\n\n<li>How to print even non-enumerable properties of an object?&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Let us consider the object cat, as shown in the listing below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var cat= {\n    name : 'foo',\n    age : 12,\n    canRun : function() {\n        console.log(\"can run\");\n    }\n}<\/pre>\n\n\n\n<p>By default, for all properties of an object enumerable is set to true. &nbsp;So right now, if we print description of cat\u2019s canRun and name properties using the Object.getOwnPropertyDescriptor, we will find enumerable value true for the both properties. &nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">console.log(Object.getOwnPropertyDescriptor(cat,\"canRun\"));\nconsole.log(Object.getOwnPropertyDescriptor(cat,'name'));<\/pre>\n\n\n\n<p>&nbsp;As you notice besides enumerable, configurable and writable are also set to true.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/community\/cfs-filesystemfile.ashx\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/dhananjay_5F00_kumar.jsproperties\/0830.img1.png\" alt=\"As you notice besides enumerable, configurable and writable are also set to true.\" title=\"As you notice besides enumerable, configurable and writable are also set to true.\"\/><\/figure>\n\n\n\n<p>Let us start with printing all properties which enumerable is true.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"printing-all-enumerable-properties-using-for-in-loop\">Printing all enumerable properties using for..in loop<\/h2>\n\n\n\n<p>We can print all enumerable properties either own or inherited of cat object using JavaScript <b>for..in<\/b> &nbsp;loop.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (var prop in cat) {\n    console.log(prop);\n}<\/pre>\n\n\n\n<p>&nbsp;Using the for..in loop, all enumerable properties can be iterated.&nbsp; Above for loop will print all the enumerable properties of cat object.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/community\/cfs-filesystemfile.ashx\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/dhananjay_5F00_kumar.jsproperties\/3073.img2.png\" alt=\"Using the for..in loop, all enumerable properties can be iterated.\" title=\"Using the for..in loop, all enumerable properties can be iterated.\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>JavaScript for..in loop does not iterate non enumerable properties or to properties which enumerable is set to <b>false<\/b>. To understand it better, let us go ahead and make enumerable of cat\u2019s name property to false using the Object.defineProperty function.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Object.defineProperty(cat, 'name', {\n    enumerable: false\n});\n\nfor (var prop in cat) {\n    console.log(prop);\n}<\/pre>\n\n\n\n<p>In the above listing, we have set enumerable of name property to false, and then iterating to the properties using the JavaScript for loop. In output, you will notice that JavaScript has omitted the name property, and will give output as shown in the image below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/community\/cfs-filesystemfile.ashx\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/dhananjay_5F00_kumar.jsproperties\/5621.img3.png\" alt=\"In output, you will notice that JavaScript has omitted the name property, and will give output as shown\" title=\"In output, you will notice that JavaScript has omitted the name property, and will give output as shown\"\/><\/figure>\n\n\n\n<p>JavaScript for..in loop iterates to all enumerable properties of an object, regardless it is their own property or inherited property. &nbsp;Let us see an example on how for..in loop returns inherited enumerable properties also. I have created an object called animal,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var animal= {\n    color: \"blue\"\n}<\/pre>\n\n\n\n<p>We are linking animal object as parent of cat object using the __proto__ . So we are creating inheritance between the cat object and the animal object as shown in the listing below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cat.__proto__ = animal;<\/pre>\n\n\n\n<p>Now, the cat object has three enumerable properties. It has its own age and canRun properties, and inherited color property. So the for..in loop will print age,canRun, and color properties for the cat object.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (var prop in cat) {\n    console.log(prop);\n}<\/pre>\n\n\n\n<p>Above for..in loop will &nbsp;print us color property besides age and canRun property.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/community\/cfs-filesystemfile.ashx\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/dhananjay_5F00_kumar.jsproperties\/0743.img4.png\" alt=\" for..in loop will \u00a0print us color property besides age and canRun property\" title=\" for..in loop will \u00a0print us color property besides age and canRun property\"\/><\/figure>\n\n\n\n<p>Below, finds entire code together used in the above discussion,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var animal= {\n    color: \"blue\"\n}\n\nvar cat= {\n    name: 'foo',\n    age: 12,\n    canRun: function () {\n        console.log(\"can run\");\n    }\n}\ncat.__proto__=animal;\n\nObject.defineProperty(cat, 'name', {\n    enumerable: false\n});\n\nfor (var prop in cat) {\n    console.log(prop);\n}<\/pre>\n\n\n\n<p>So we can summarize for..in loop in the following points,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It returns object\u2019s all own enumerable properties<\/li>\n\n\n\n<li>It returns object\u2019s all inherited enumerable properties<\/li>\n\n\n\n<li>It does not return non-enumerable properties<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"printing-all-own-enumerable-properties-using-object-keys\">Printing all own enumerable properties using Object.keys<\/h2>\n\n\n\n<p>In ECMA 5, Object.keys() got introduced, which returns an array containing all enumerable properties of the JavaScript object. It only returns object\u2019s own properties; it does not return inherited properties.<\/p>\n\n\n\n<p>We can print all enumerable properties of cat object using Objecy.keys, as shown in the listing below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var cat= {\n\n    name: 'foo',\n    age: 12,\n    canRun: function () {\n        console.log(\"can run\");\n    }\n}\n\nconsole.log(Object.keys(cat));<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"printing-all-methods-of-an-object\">Printing all methods of an object <\/h2>\n\n\n\n<p>We may have other requirement, just to print all own methods of an object. &nbsp;To do so, let us create a function, which will return all own methods of an object.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function getAllMethods(object) {\n    return Object.getOwnPropertyNames(object).filter(function (p) {\n            return typeof object[p]=='function';\n        });\n}<\/pre>\n\n\n\n<p>getAllMethods() function will return all the methods of the passed object. Let us say we have an object cat as shown in the next listing,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var cat= {\n    name: 'foo',\n    age: 12,\n    canRun: function () {\n        console.log(\"can run\");\n    }\n}<\/pre>\n\n\n\n<p>To print methods of cat object, we will pass it to the getAllMethods() function, and function will return canRun method.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">console.log(getAllMethods(cat));<\/pre>\n\n\n\n<p>In the output we can see canRun printed.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/community\/cfs-filesystemfile.ashx\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/dhananjay_5F00_kumar.jsproperties\/2084.img5.png\" alt=\"In the output we can see canRun printed\" title=\"In the output we can see canRun printed\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"conclusion\">Conclusion <\/h2>\n\n\n\n<p>In this post we learnt about various ways of printing or enumerating object\u2019s own or inherited properties and methods. I hope you find this post useful. Thanks for reading. &nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we will explore all the options to iterate and print properties of a JavaScript object.<\/p>\n","protected":false},"author":65,"featured_media":2210,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-592","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=592"}],"version-history":[{"count":2,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/592\/revisions"}],"predecessor-version":[{"id":1938,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/592\/revisions\/1938"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/2210"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}