- Published on
javascript this keyword
- Authors
- Name
- Ganesh Negi
javascript this keyword

The this keyword in JavaScript is a special reference that points to the context in which a function is executed. Its value is dynamic and depends on how a function is called rather than where it is written. This makes this a powerful but sometimes tricky concept to grasp.
How this Works in Different Contexts
Global Context When this is used in the global scope (outside of any function), it refers to the global object: In a browser environment, this refers to window. In Node.js, it points to the global object.
Function Context In regular (non-arrow) functions, this depends on how the function is invoked: If a function is called as part of an object (a method), this refers to that object. If called independently, this defaults to the global object (window in browsers) in non-strict mode or undefined in strict mode.
Constructor Functions When a function is used as a constructor (called with new), this points to the new instance being created.
Arrow Functions Unlike regular functions, arrow functions do not have their own this. Instead, they inherit this from their surrounding lexical scope. This means the value of this inside an arrow function is determined by where the arrow function is defined, not how it is called.
Event Handlers In event listeners, this generally refers to the element that triggered the event. However, if an arrow function is used, it inherits this from the outer scope instead.
Explicit Binding (call(), apply(), bind()) JavaScript provides methods to manually control the value of this:
call(): Invokes a function and explicitly sets this to a specified object. apply(): Works like call(), but passes arguments as an array. bind(): Returns a new function with this permanently set to a given value.
Examples of this in Action
Object Method Example
const user = {
name: 'interviewpi',
greet() {
console.log(`Hi, my name is ${this.name}!`);
},
};
user.greet(); // Output: "Hi, my name is interviewpi!"
Here, this refers to the user object because greet is called as a method of user.
Standalone Function
function sayHello() {
console.log(`Hello, my name is ${this.name}!`);
}
sayHello(); // Output: "Hello, my name is undefined!" (in non-strict mode)
Since sayHello is called without an object, this defaults to the global object (window in browsers), which does not have a name property.
Using call() for Explicit Binding
function introduce() {
console.log(`Hello, my name is ${this.name}!`);
}
const person = { name: 'Ethan' };
introduce.call(person); // Output: "Hello, my name is Ethan!"
By using call(), we explicitly set this to refer to person, ensuring access to the name property.
Key Takeaways
this dynamically depends on how a function is called, not where it is written. In the global scope, this refers to window (or global in Node.js). In an object method, this refers to the object that owns the method. In a constructor function, this points to the newly created object. Arrow functions inherit this from their surrounding scope. call(), apply(), and bind() allow explicit control over this.
Understanding this is crucial for writing clean, efficient, and bug-free JavaScript code. Mastering its behavior will help you navigate object-oriented programming patterns and work effectively with JavaScript frameworks and libraries.