{"id":43095,"date":"2024-03-02T12:25:25","date_gmt":"2024-03-02T06:55:25","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=43095"},"modified":"2025-10-27T12:37:29","modified_gmt":"2025-10-27T07:07:29","slug":"guide-for-this-keyword-in-javascript","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/guide-for-this-keyword-in-javascript\/","title":{"rendered":"Complete Guide to this Keyword in JavaScript"},"content":{"rendered":"\n<p>The <strong>this<\/strong> keyword is a fundamental aspect of JavaScript that is often misunderstood by developers, especially beginners. <\/p>\n\n\n\n<p>It is a special reference variable that plays a crucial role in determining the context in which a function is called. <\/p>\n\n\n\n<p>In this comprehensive guide, we will explore the various contexts in which <strong>this<\/strong> keyword is used and how it can be effectively utilized in your JavaScript projects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is this keyword in JavaScript?<\/h2>\n\n\n\n<p>The <strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/This_(computer_programming)\" target=\"_blank\" rel=\"noreferrer noopener\">this<\/a><\/strong> keyword in JavaScript is a reference variable that is automatically assigned a value when a function is called. <\/p>\n\n\n\n<p>It refers to the object that is executing the current piece of JavaScript code. The value of <strong>this<\/strong> is not determined by how or where a function is declared, but by how it is called &#8211; the call-site. <\/p>\n\n\n\n<p>This dynamic nature of <strong>this<\/strong> keyword can lead to confusion, but with a clear understanding, it becomes a powerful tool.<\/p>\n\n\n\n<p><strong><em>Must Read: <a href=\"https:\/\/www.guvi.in\/blog\/javascript-frontend-roadmap\/\">Master JavaScript Frontend Roadmap: From Novice to Expert <\/a><\/em><\/strong><\/p>\n\n\n\n<p>Before we move to the next section, make sure that you are strong in the full-stack development basics. If not, consider enrolling for a professionally <strong><a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=this+keyword+in+javascript\" target=\"_blank\" rel=\"noreferrer noopener\">certified online full-stack web development course<\/a><\/strong> by a recognized institution that can also offer you an industry-grade certificate that boosts your resume. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The different contexts in which the &#8216;this&#8217; keyword is used<\/h2>\n\n\n\n<p>As discussed above, the dynamic nature of &#8216;this&#8217; can be pretty confusing, <a href=\"https:\/\/www.guvi.in\/blog\/javascript-roadmap-for-beginners\/\" target=\"_blank\" rel=\"noreferrer noopener\">especially for beginners<\/a> as the value of &#8216;this&#8217; can change depending on the context. So let&#8217;s discuss how you can use it based on different contexts:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. The Global Context<\/h3>\n\n\n\n<p>When the &#8216;<strong>this&#8217;<\/strong> keyword is used outside of any function or object, it refers to the global object. In a browser environment, the global object is the <strong>window<\/strong> object. However, it&#8217;s important to note that using <strong>this<\/strong> in the global context can lead to hard-to-debug issues, especially when dealing with strict modes or modules, where <strong>this<\/strong> is undefined.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>console.log(this); \/\/ Outputs: Window {...} (or the global object in a non-browser environment)<\/strong><\/code><\/pre>\n\n\n\n<p><strong>Also Read: <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-arrays-in-javascript\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/guide-for-arrays-in-javascript\/\" rel=\"noreferrer noopener\">Arrays in JavaScript: A Comprehensive Guide<\/a><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. The Function Context<\/h3>\n\n\n\n<p>The value of <strong>this<\/strong> inside a function depends on how the function is called. When a function is called directly, without any specific context, <strong>this<\/strong> keyword refers to the global object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>function myFunction() {\n  console.log(this);\n}\n\nmyFunction(); \/\/ Outputs: Window {...} (or the global object in a non-browser environment)<\/strong><\/code><\/pre>\n\n\n\n<p>However, the value of<strong> this<\/strong> changes when a function is invoked as a method of an object. In this case, <strong>this<\/strong> refers to the object itself.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>let myObject = {\n  myMethod: function() {\n    console.log(this);\n  }\n};\n\nmyObject.myMethod(); \/\/ Outputs: {myMethod: \u0192}<\/strong><\/code><\/pre>\n\n\n\n<p><strong>Read More: <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-functions-in-javascript\/\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/guide-for-functions-in-javascript\/\">Functions in JavaScript: Important Things To Know<\/a><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. The Constructor Context<\/h3>\n\n\n\n<p>When a function is invoked with the <strong>new<\/strong> keyword, it is called a constructor function. In this context, <strong>this<\/strong> keyword inside the function refers to the newly created object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>function MyConstructor() {\n  this.myProperty = \"Hello World, this is HCL GUVI\";\n}\n\nlet myInstance = new MyConstructor();\nconsole.log(myInstance.myProperty); \/\/ Outputs: \"Hello World, this is HCL GUVI\"<\/strong><\/code><\/pre>\n\n\n\n<p>In the above example, <strong>this<\/strong> keyword inside the <strong>MyConstructor<\/strong> function refers to the <strong>myInstance<\/strong> object.<\/p>\n\n\n\n<p><strong>Must Read: <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-variables-and-data-types-in-javascript\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/guide-for-variables-and-data-types-in-javascript\/\" rel=\"noreferrer noopener\">Variables and Data Types in JavaScript: A Complete Guide<\/a><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Arrow Functions and this<\/h3>\n\n\n\n<p>Arrow functions handle <strong>this<\/strong> differently compared to regular functions. They do not create their execution context but instead inherit <strong>this<\/strong> from the enclosing context. This is known as lexical scoping.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>let myObject = {\n  myMethod: function() {\n    let arrowFunction = () =&gt; console.log(this);\n    arrowFunction();\n  }\n};\n\nmyObject.myMethod(); \/\/ Outputs: {myMethod: \u0192}<\/strong><\/code><\/pre>\n\n\n\n<p>In this example, even though <strong>arrowFunction<\/strong> is a function, <strong>this<\/strong> keyword inside it refers to <strong>myObject<\/strong> because it inherits <strong>&#8216;this&#8217;<\/strong> from the parent function.<\/p>\n\n\n\n<p><strong><em>Must Explore: <a href=\"https:\/\/www.guvi.in\/blog\/java-vs-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java vs JavaScript: Top 3 Comparisons<\/a><\/em><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Changing the Context with call(), apply(), and bind()<\/h3>\n\n\n\n<p>JavaScript provides three methods &#8211; <strong>call()<\/strong>, <strong>apply()<\/strong>, and <strong>bind()<\/strong> &#8211; that allow you to explicitly set the value of <strong>&#8216;this&#8217;<\/strong> in a function. These methods are useful when you want to control the context in which a function is called.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The call() and apply() Methods<\/h4>\n\n\n\n<p>The <strong>call()<\/strong> and <strong>apply()<\/strong> methods allow you to call a function with a given <strong>this<\/strong> value and arguments. The difference between the two is that <strong>call()<\/strong> accepts an argument list, while <strong>apply()<\/strong> accepts a single array of arguments.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>function myFunction() {\n  console.log(this);\n}\n\nlet myObject = {};\n\nmyFunction.call(myObject); \/\/ Outputs: {}\nmyFunction.apply(myObject); \/\/ Outputs: {}<\/strong><\/code><\/pre>\n\n\n\n<p>In the above examples, we are using <strong>call()<\/strong> and <strong>apply()<\/strong> to set <strong><strong>this<\/strong><\/strong> inside <strong>myFunction<\/strong> to <strong>myObject<\/strong>.<\/p>\n\n\n\n<p><strong>Also Explore: <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-constructors-in-javascript\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/guide-for-constructors-in-javascript\/\" rel=\"noreferrer noopener\">Constructors in JavaScript: 6 Uses Every Top Programmer Must Know<\/a><\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The bind() Method<\/h4>\n\n\n\n<p>The <strong>bind()<\/strong> method returns a new function where <strong><strong>this<\/strong><\/strong> has a certain value. It allows you to create a function with a permanently bound <strong>this<\/strong> value, which can be useful in certain scenarios.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>function myFunction() {\n  console.log(this);\n}\n\nlet myObject = {};\n\nlet boundFunction = myFunction.bind(myObject);\nboundFunction(); \/\/ Outputs: {}<\/strong><\/code><\/pre>\n\n\n\n<p>In the above example, we use <strong>bind()<\/strong> to create a new function <strong>boundFunction<\/strong> where <strong>this<\/strong> is set to <strong>myObject<\/strong>. When we call <strong>boundFunction<\/strong>, it outputs <strong>myObject<\/strong> as the value of <strong><strong>this<\/strong><\/strong>.<\/p>\n\n\n\n<p><strong>Also Read: <a href=\"https:\/\/www.guvi.in\/blog\/javascript-ides-and-code-editors-you-must-know\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/javascript-ides-and-code-editors-you-must-know\/\" rel=\"noreferrer noopener\">Top 10 JavaScript IDEs and Code Editors You Should Know<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Using the &#8216;<strong>this<\/strong>&#8216; Keyword in JavaScript<\/h2>\n\n\n\n<p>When working with the <strong>this<\/strong> keyword in JavaScript, it&#8217;s important to follow some best practices to avoid confusion and potential bugs:<\/p>\n\n\n\n<ol>\n<li>Use explicit binding with&nbsp;<strong>call()<\/strong>,&nbsp;<strong>apply()<\/strong>, or&nbsp;<strong>bind()<\/strong>&nbsp;when you need to control the context in which a function is called.<\/li>\n\n\n\n<li>Be aware of the default binding of&nbsp;<strong>this<\/strong>&nbsp;in different contexts, such as the global scope or function scope.<\/li>\n\n\n\n<li>Use arrow functions when you want to maintain the lexical scope of&nbsp;<strong><strong>this<\/strong><\/strong>.<\/li>\n\n\n\n<li>Avoid using <strong>this<\/strong>&nbsp;keyword in the global scope, as it can lead to unexpected behavior.<\/li>\n\n\n\n<li>Use strict mode to catch potential errors related to&nbsp;<strong>this<\/strong>&nbsp;being undefined in certain contexts.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes to Avoid<\/h2>\n\n\n\n<p>When working with <strong>this<\/strong> keyword, it&#8217;s easy to make mistakes that can result in unexpected behavior. Here are some common mistakes to avoid:<\/p>\n\n\n\n<ol>\n<li>Forgetting to use the&nbsp;<strong>new<\/strong>&nbsp;keyword when invoking a constructor function.<\/li>\n\n\n\n<li>Confusing the context of&nbsp;<strong>this<\/strong>&nbsp;in nested functions or callback functions.<\/li>\n\n\n\n<li>Using arrow functions when you need the dynamic binding of&nbsp;<strong><strong>this<\/strong><\/strong>.<\/li>\n\n\n\n<li>Using&nbsp;<strong>call()<\/strong>&nbsp;or&nbsp;<strong>apply()<\/strong>&nbsp;without providing the correct arguments.<\/li>\n\n\n\n<li>Assuming that&nbsp;<strong><strong>this<\/strong><\/strong>&nbsp;always refers to the object that a function is defined in.<\/li>\n<\/ol>\n\n\n\n<p><strong><em>Start Developing: <a href=\"https:\/\/www.guvi.in\/blog\/best-javascript-project-ideas\/\" target=\"_blank\" rel=\"noreferrer noopener\">30 Best JavaScript Project Ideas For You [3 Bonus Portfolio Projects]<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced Techniques with <strong>this<\/strong> keyword<\/h2>\n\n\n\n<p>The <strong>&#8216;this&#8217;<\/strong> keyword in JavaScript can be leveraged in advanced techniques to achieve more complex functionality. Here are a few examples:<\/p>\n\n\n\n<ol>\n<li>Method chaining: By returning&nbsp;<strong>this<\/strong>&nbsp;at the end of a method, you can chain multiple method calls together.<\/li>\n\n\n\n<li>Currying: Using&nbsp;<strong>bind()<\/strong>, you can create a new function with some arguments pre-set, which can be useful for partial function application.<\/li>\n\n\n\n<li>Event handling: The&nbsp;<strong><strong>this<\/strong><\/strong>&nbsp;keyword can be used in event handlers to refer to the element on which the event is triggered.<\/li>\n\n\n\n<li>Prototype modification: By using <strong><strong>this<\/strong><\/strong>&nbsp;keyword inside a constructor function or a class method, you can modify the prototype of an object.<\/li>\n<\/ol>\n\n\n\n<p><strong>Also, Explore <a href=\"https:\/\/www.guvi.in\/blog\/best-javascript-frameworks\/\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/best-javascript-frameworks\/\">About Best JavaScript Frameworks<\/a><\/strong><\/p>\n\n\n\n<p class=\"has-text-align-center\"><em><strong>If you want to learn more about programming with JavaScript and make a successful career out of it, then you must sign up for the <a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=this+keyword+in+javascript\" target=\"_blank\" rel=\"noreferrer noopener\">NSDC(Skill India) Certified Full Stack Development Course<\/a>, offered by HCL GUVI, which gives you in-depth knowledge of the practical implementation of all frontend as well as <a href=\"https:\/\/www.guvi.in\/blog\/guide-on-backend-development\/\" target=\"_blank\" rel=\"noreferrer noopener\">backend development<\/a> through various real-life FSD projects.<\/strong><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up&#8230;<\/h2>\n\n\n\n<p>Understanding the power of <strong>this<\/strong> keyword is essential for mastering JavaScript. It allows you to control the context in which functions are called and enables advanced techniques such as method chaining and currying. <\/p>\n\n\n\n<p>By following best practices and avoiding common mistakes, you can harness the full potential of <strong>this<\/strong> keyword in your JavaScript projects.<\/p>\n\n\n\n<p>Now that you have a solid understanding of <strong>this<\/strong> keyword in JavaScript, it&#8217;s time to apply this knowledge to your projects and explore more advanced techniques.<\/p>\n\n\n\n<p><strong>Must Read: <a href=\"https:\/\/www.guvi.in\/blog\/best-reasons-to-learn-javascript\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/best-reasons-to-learn-javascript\/\" rel=\"noreferrer noopener\">7 Best Reasons to Learn JavaScript | 1 Bonus Point<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1709188248820\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is <strong>this<\/strong> keyword in JavaScript?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>In JavaScript, <strong>&#8216;this&#8217;<\/strong> is a keyword that refers to the context in which a function is called. It is a reference to the object that the function is a method of.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709188430035\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Does <strong>this<\/strong> keyword refer to the same thing in all contexts?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No, the value of <strong>&#8216;this&#8217;<\/strong> is not static and can change depending on the context in which it is called.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709188432048\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How does <strong>this<\/strong> keyword work in arrow functions?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Arrow functions do not create their context for <strong>&#8216;this&#8217;<\/strong>. Instead, they inherit <strong>&#8216;this&#8217;<\/strong> from the enclosing context.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709188433518\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is a callback in JS?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A callback in JavaScript is a function passed as an argument to another function, which is then invoked inside the outer function to complete some kind of action or operation.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>The this keyword is a fundamental aspect of JavaScript that is often misunderstood by developers, especially beginners. It is a special reference variable that plays a crucial role in determining the context in which a function is called. In this comprehensive guide, we will explore the various contexts in which this keyword is used and [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":43463,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[294],"tags":[],"views":"4914","authorinfo":{"name":"Jaishree Tomar","url":"https:\/\/www.guvi.in\/blog\/author\/jaishree\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/03\/Feature-Image-Complete-Guide-to-the-this-Keyword-in-JavaScript-300x112.webp","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/03\/Feature-Image-Complete-Guide-to-the-this-Keyword-in-JavaScript.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/43095"}],"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\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=43095"}],"version-history":[{"count":36,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/43095\/revisions"}],"predecessor-version":[{"id":91344,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/43095\/revisions\/91344"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/43463"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=43095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=43095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=43095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}