{"id":59319,"date":"2024-09-10T12:00:22","date_gmt":"2024-09-10T06:30:22","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=59319"},"modified":"2025-10-24T15:33:33","modified_gmt":"2025-10-24T10:03:33","slug":"the-concept-of-this-keyword","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/the-concept-of-this-keyword\/","title":{"rendered":"The concept of \u201cthis\u201d keyword in Javascript"},"content":{"rendered":"\n<p>If you are starting out as a full-stack developer, you definitely come across one of the most popular languages which is JavaScript. There are various keywords like &#8220;this&#8221; keyword in JavaScript that play a vital role in backend development. <\/p>\n\n\n\n<p>In those JavaScript keywords, the this keyword is something that every developer should know! If you don&#8217;t know what it is or how to use it, this article is right there to help you! So, let us get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is &#8220;this&#8221; Keyword? How to Use it?<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/hub\/javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript<\/a>, one of the most popular programming languages, is widely used for building dynamic web applications. <\/p>\n\n\n\n<p>One of its unique and sometimes confusing features is the &#8220;this&#8221; keyword. Understanding how this works is essential for <a href=\"https:\/\/www.guvi.in\/blog\/best-javascript-practices-for-developers\/\" target=\"_blank\" rel=\"noreferrer noopener\">mastering JavaScript<\/a>, as it behaves differently than in many other programming languages. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is &#8220;this&#8221;?<\/strong><\/h3>\n\n\n\n<p>In JavaScript, &#8220;this&#8221; refers to the context in which a function is executed. It is a reference to an object, and its value depends on where and how the function is called.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Global Context<\/strong><\/h3>\n\n\n\n<p>When this is used outside of any function, in the global context, it refers to the global object. In browsers, the global object is &#8220;window&#8221;.<\/p>\n\n\n\n<p><strong><code>console.log(this); \/\/ window<\/code><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Function Context<\/strong><\/h3>\n\n\n\n<p>When &#8220;this&#8221; is used inside a regular <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-functions-in-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">function in JavaScript<\/a>, its value depends on how the function is called.<\/p>\n\n\n\n<ol>\n<li><strong>Simple Function Call<\/strong><\/li>\n<\/ol>\n\n\n\n<p>In a simple function call, this refers to the global object. Let us see about this with an example code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function showThis() {\n\n&nbsp;&nbsp;console.log(this);\n\n}\n\nshowThis(); \/\/ window<\/code><\/pre>\n\n\n\n<ol start=\"2\">\n<li><strong>Method Call<\/strong><\/li>\n<\/ol>\n\n\n\n<p>When a function is called as a <a href=\"https:\/\/playcode.io\/javascript\/methods\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">method of an object in JavaScript<\/a>, this refers to the object that owns the method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const obj = {\n\n&nbsp;&nbsp;name: 'John',\n\n&nbsp;&nbsp;greet: function() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;console.log(this.name);\n\n&nbsp;&nbsp;}\n\n};\n\nobj.greet(); \/\/ John<\/code><\/pre>\n\n\n\n<ol start=\"3\">\n<li><strong>Constructor Call<\/strong><\/li>\n<\/ol>\n\n\n\n<p>When a function is used as a constructor with the new keyword, &#8220;this&#8221; refers to the newly created instance. Here is an example code to showcase this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Person(name) {\n\n\u00a0\u00a0this.name = name;\n\n}\n\nconst person = new Person('HCL Guvi');\n\nconsole.log(person.name); \/\/ HCL Guvi<\/code><\/pre>\n\n\n\n<ol start=\"4\">\n<li><strong>Arrow Functions<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Arrow functions behave differently. They do not have their own &#8220;this&#8221; context; instead, they inherit this from the surrounding non-arrow function. The example below helps you understand more about the arrow functions<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const obj = {\n\n\u00a0\u00a0name: 'HCL Guvi',\n\n\u00a0\u00a0greet: function() {\n\n\u00a0\u00a0\u00a0\u00a0const arrowFunction = () => console.log(this.name);\n\n\u00a0\u00a0\u00a0\u00a0arrowFunction();\n\n\u00a0\u00a0}\n\n};\n\nobj.greet(); \/\/ HCL Guvi<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Explicit Binding<\/strong><\/h3>\n\n\n\n<p>JavaScript provides methods to explicitly set the value of this: call(), apply(), and bind().<\/p>\n\n\n\n<ol>\n<li><strong>call() and apply()<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Both call() and apply() methods are used to call a function with a specified &#8220;this&#8221; value. The difference between them is that call() takes arguments separately, while apply() takes them as an array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function introduce(language) {\n\n&nbsp;&nbsp;console.log(`My name is ${this.name} and I speak ${language}.`);\n\n}\n\nconst person = { name: 'Carlos' };\n\nintroduce.call(person, 'Spanish'); \/\/ My name is Carlos and I speak Spanish.\n\nintroduce.apply(person, &#91;'Spanish']); \/\/ My name is Carlos and I speak Spanish.<\/code><\/pre>\n\n\n\n<ol start=\"2\">\n<li><strong>bind()<\/strong><\/li>\n<\/ol>\n\n\n\n<p>The bind() method creates a new function that, when called, has its this keyword set to the provided value. This is useful for creating functions with a specific &#8220;this&#8221; value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const person = { name: 'Diana' };\n\nfunction greet() {\n\n&nbsp;&nbsp;console.log(this.name);\n\n}\n\nconst boundGreet = greet.bind(person);\n\nboundGreet(); \/\/ Diana<\/code><\/pre>\n\n\n\n<p>We hope you understand the ways in which &#8220;this&#8221; keyword is used in <a href=\"https:\/\/www.guvi.in\/blog\/best-front-end-javascript-frameworks\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript frameworks<\/a>. In case, you want to learn more about &#8220;this&#8221; and gain in-depth knowledge on full-stack development, consider enrolling for HCL GUVI&#8217;s certified <a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=this-keyword\" target=\"_blank\" rel=\"noreferrer noopener\">Full-stack Development Course<\/a> that teaches you everything from scratch and make sure you master it!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In conclusion, understanding the &#8220;this&#8221; keyword in JavaScript is crucial for writing effective and bug-free code. By knowing how this behaves in different contexts, you can avoid common pitfalls and harness its power to create more dynamic and context-aware functions. <\/p>\n\n\n\n<p>Whether you&#8217;re working with global context, object methods, constructors, or arrow functions, mastering this will significantly enhance your JavaScript skills.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are starting out as a full-stack developer, you definitely come across one of the most popular languages which is JavaScript. There are various keywords like &#8220;this&#8221; keyword in JavaScript that play a vital role in backend development. In those JavaScript keywords, the this keyword is something that every developer should know! If you [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":64314,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[294,429],"tags":[],"views":"3600","authorinfo":{"name":"swathy s","url":"https:\/\/www.guvi.in\/blog\/author\/swathy-s\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/09\/the_concept_of_this_keyword_in_javascript_1x-1-300x116.webp","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/09\/the_concept_of_this_keyword_in_javascript_1x-1.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/59319"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=59319"}],"version-history":[{"count":11,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/59319\/revisions"}],"predecessor-version":[{"id":91169,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/59319\/revisions\/91169"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/64314"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=59319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=59319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=59319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}