Skip to content

this keyword

The keyword this inside a function refers to the current execution context or the object that the function is being called on.

In JavaScript, the value of this is determined dynamically at runtime, based on how the function is invoked. Its value can vary depending on the context in which the function is called.

Here are a few common scenarios:

  1. Global Scope:
  2. If a function is called in the global scope (outside of any object), this refers to the global object, which is window in a browser environment or global in Node.js.

  3. Object Method:

  4. When a function is called as a method of an object, this refers to the object itself. It allows the function to access and manipulate the object's properties and methods.

  5. Constructor Function:

  6. Inside a constructor function, this refers to the newly created instance of the object being constructed. It allows the constructor function to set properties and behavior on the newly created object.

  7. Event Handlers:

  8. When a function is used as an event handler, this typically refers to the element that triggered the event.

It's important to note that the value of this is not lexically scoped like regular variables. It is dynamically determined each time a function is called.

To summarize, this provides a way to refer to the current object or context within a function, allowing you to access and manipulate properties and behavior associated with that object.