On this page

The this Keyword and Execution Context

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

The this keyword is one of the most powerful yet frequently misunderstood features of JavaScript. Unlike many other programming languages where this always refers to the instance of a class, in JavaScript, the value of this is dynamic. It is determined entirely by the execution context—specifically, how a function is invoked, rather than where it is defined.

Understanding Execution Context​

In JavaScript, every time code runs, it does so within an execution context. You can think of the this keyword as a reference to the "owner" of the function currently being executed. To identify what this refers to, you must look at the call site (the location in the code where the function is called).

Global Context​

When code is executed in the outermost level of a script, outside of any function, this refers to the global object.


console.log(this); // In a browser, this outputs the Window object



function checkThis() {

  return this;

}



console.log(checkThis()); // In non-strict mode: Window. In strict mode: undefined.

Object Method Binding​

When a function is called as a method of an object, this is bound to the object that "owns" the method. This is often referred to as Implicit Binding. This allow methods to access and manipulate other properties within the same object.


const userProfile = {

  username: "ProfessorJS",

  greet: function () {

    console.log("Hello, my name is " + this.username);

  },

};



userProfile.greet(); // Output: Hello, my name is ProfessorJS

In this scenario, this points to userProfile because the function was called using the dot notation (userProfile.greet()).

The "Lost" Context Problem​

A common source of confusion occurs when a method is extracted from its object and assigned to a variable. When you call that variable as a standalone function, the connection to the original object is severed, and this reverts to the global object (or undefined).


const detachedGreet = userProfile.greet;

detachedGreet(); // Output: Hello, my name is undefined

Because detachedGreet() is called without a leading object reference, the execution context changes, and this no longer points to userProfile.

Constructor Context​

When a function is used as a constructor with the new keyword, a brand-new object is created. In this context, this is bound to the newly created instance.


function Smartphone(brand) {

  this.brand = brand;

}



const myPhone = new Smartphone("Apple");

console.log(myPhone.brand); // Output: Apple

The new keyword explicitly redirects the this binding to the empty object it just generated.

Explicit Binding: call, apply, and bind​

JavaScript provides three methods that allow developers to manually set the value of this, regardless of how the function is called. This is known as Explicit Binding.


function describe(price, currency) {

  console.log(this.item + " costs " + price + currency);

}



const product = { item: "Laptop" };



describe.call(product, 1200, "USD"); // Immediate execution

const boundDescribe = describe.bind(product); // Returns a new function

boundDescribe(1200, "USD"); // Execution later

The Exception: Arrow Functions​

It is important to note that Arrow Functions behave differently. They do not have their own this binding. Instead, they inherit this from the surrounding lexical scope (the code that contains the arrow function). This makes them particularly useful for callbacks and timers where you want to maintain the context of the parent object.

Technical Precision Checklist​

To accurately predict the value of this, apply these rules in order of priority:

  1. New Binding: Is the function called with new? If so, this is the new object.
  2. Explicit Binding: Is the function called with call, apply, or bind? If so, this is the specified object.
  3. Implicit Binding: Is the function called as a method (e.g., obj.method())? If so, this is that object.
  4. Default Binding: Is it a standalone function call? If so, this is the global object (or undefined in strict mode).

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Objects

Next

Prototypes & Inheritance