{"id":411,"date":"2023-05-29T18:22:10","date_gmt":"2023-05-29T18:22:10","guid":{"rendered":"https:\/\/www.devopsconsulting.in\/blog\/?p=411"},"modified":"2023-05-29T18:22:11","modified_gmt":"2023-05-29T18:22:11","slug":"this-in-javascript","status":"publish","type":"post","link":"https:\/\/www.devopsconsulting.in\/blog\/this-in-javascript\/","title":{"rendered":"this in JavaScript"},"content":{"rendered":"\n<p>The <code>this<\/code> keyword in JavaScript refers to the object that is currently executing the code. It holds the reference to a single object and represents the current execution context of the JavaScript program. Functions in JavaScript are essentially objects, which means they can be assigned to variables, passed to other functions, and returned from functions. Similar to objects, functions have their own properties.<\/p>\n\n\n\n<p>The value of <code>this<\/code> can change depending on how a function is defined, how it is invoked, and the default execution context. When used inside a function, the <code>this<\/code> value will vary depending on the specific circumstances. It refers to different objects depending on how it is used.<\/p>\n\n\n\n<p><strong>The\u00a0<code>this<\/code>\u00a0keyword refers to different objects depending on how it is used:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In an object method, this refers to the object.<\/li>\n\n\n\n<li>Alone, this refers to the global object.<\/li>\n\n\n\n<li>In a function, this refers to the global object.<\/li>\n\n\n\n<li>In a function, in strict mode, this is undefined.<\/li>\n\n\n\n<li>In an event, this refers to the element that received the event.<\/li>\n\n\n\n<li>Methods like call(), apply(), and bind() can refer this to any object.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Global Scope\r\nconsole.log(this); \/\/ Output: Window (in browser) or global (in Node.js)\r\n\r\n\/\/ Object Method\r\nconst myObject = {\r\n  property: 'value',\r\n  myMethod() {\r\n    console.log(this.property);\r\n  }\r\n};\r\n\r\nmyObject.myMethod(); \/\/ Output: \"value\"\r\n\r\n\/\/ Constructor Function\r\nfunction MyClass(value) {\r\n  this.property = value;\r\n}\r\n\r\nconst myInstance = new MyClass('example');\r\nconsole.log(myInstance.property); \/\/ Output: \"example\"\r\n\r\n\/\/ Event Handlers\r\nconst button = document.querySelector('button');\r\nbutton.addEventListener('click', function() {\r\n  console.log(this); \/\/ Output: DOM button element\r\n});<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the global scope, this refers to the global object (Window in a browser or global in Node.js).<\/li>\n\n\n\n<li>Inside the myMethod method of the myObject object, this refers to the myObject object itself. Therefore, this.property accesses the property value of the object.<\/li>\n\n\n\n<li>In the constructor function MyClass, this refers to the newly created instance of the object when using the new keyword. The property value is assigned to the property property of the instance.<\/li>\n\n\n\n<li>Inside the event handler function assigned to the button&#8217;s click event, this refers to the DOM element representing the button that triggered the event.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The this keyword in JavaScript refers to the object that is currently executing the code. It holds the reference to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[85],"class_list":["post-411","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-this-in-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>this in JavaScript - DevOps Consulting<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.devopsconsulting.in\/blog\/this-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"this in JavaScript - DevOps Consulting\" \/>\n<meta property=\"og:description\" content=\"The this keyword in JavaScript refers to the object that is currently executing the code. It holds the reference to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsconsulting.in\/blog\/this-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Consulting\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-29T18:22:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-29T18:22:11+00:00\" \/>\n<meta name=\"author\" content=\"Abhishek Singh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Abhishek Singh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.devopsconsulting.in\/blog\/this-in-javascript\/\",\"url\":\"https:\/\/www.devopsconsulting.in\/blog\/this-in-javascript\/\",\"name\":\"this in JavaScript - DevOps Consulting\",\"isPartOf\":{\"@id\":\"https:\/\/www.devopsconsulting.in\/blog\/#website\"},\"datePublished\":\"2023-05-29T18:22:10+00:00\",\"dateModified\":\"2023-05-29T18:22:11+00:00\",\"author\":{\"@id\":\"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.devopsconsulting.in\/blog\/this-in-javascript\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.devopsconsulting.in\/blog\/#website\",\"url\":\"https:\/\/www.devopsconsulting.in\/blog\/\",\"name\":\"DevOps Consulting\",\"description\":\"DevOps Consulting | SRE Consulting | DevSecOps Consulting | MLOps Consulting\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.devopsconsulting.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f\",\"name\":\"Abhishek Singh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g\",\"caption\":\"Abhishek Singh\"},\"description\":\"I\u2019m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I\u2019ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains: \u2022 DevOps School \u2013 Tech blogs and tutorials \u2022 Holiday Landmark \u2013 Travel stories and guides \u2022 Stocks Mantra \u2013 Stock market strategies and tips \u2022 My Medic Plus \u2013 Health and fitness guidance \u2022 TrueReviewNow \u2013 Honest product reviews \u2022 Wizbrand \u2013 SEO and digital tools for businesses I\u2019m also exploring the fascinating world of Quantum Computing.\",\"url\":\"https:\/\/www.devopsconsulting.in\/blog\/author\/abhishek\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"this in JavaScript - DevOps Consulting","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.devopsconsulting.in\/blog\/this-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"this in JavaScript - DevOps Consulting","og_description":"The this keyword in JavaScript refers to the object that is currently executing the code. It holds the reference to [&hellip;]","og_url":"https:\/\/www.devopsconsulting.in\/blog\/this-in-javascript\/","og_site_name":"DevOps Consulting","article_published_time":"2023-05-29T18:22:10+00:00","article_modified_time":"2023-05-29T18:22:11+00:00","author":"Abhishek Singh","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Abhishek Singh","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.devopsconsulting.in\/blog\/this-in-javascript\/","url":"https:\/\/www.devopsconsulting.in\/blog\/this-in-javascript\/","name":"this in JavaScript - DevOps Consulting","isPartOf":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/#website"},"datePublished":"2023-05-29T18:22:10+00:00","dateModified":"2023-05-29T18:22:11+00:00","author":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsconsulting.in\/blog\/this-in-javascript\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.devopsconsulting.in\/blog\/#website","url":"https:\/\/www.devopsconsulting.in\/blog\/","name":"DevOps Consulting","description":"DevOps Consulting | SRE Consulting | DevSecOps Consulting | MLOps Consulting","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopsconsulting.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f","name":"Abhishek Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g","caption":"Abhishek Singh"},"description":"I\u2019m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I\u2019ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains: \u2022 DevOps School \u2013 Tech blogs and tutorials \u2022 Holiday Landmark \u2013 Travel stories and guides \u2022 Stocks Mantra \u2013 Stock market strategies and tips \u2022 My Medic Plus \u2013 Health and fitness guidance \u2022 TrueReviewNow \u2013 Honest product reviews \u2022 Wizbrand \u2013 SEO and digital tools for businesses I\u2019m also exploring the fascinating world of Quantum Computing.","url":"https:\/\/www.devopsconsulting.in\/blog\/author\/abhishek\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/comments?post=411"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/411\/revisions"}],"predecessor-version":[{"id":413,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/411\/revisions\/413"}],"wp:attachment":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/media?parent=411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/categories?post=411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/tags?post=411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}