The this keyword is a reference to an Execution Context object. Its value is determined dynamically at the moment the function is executed.
There are 4 Rules of Binding that determine what this points to.
If a function is called as a standalone function, this refers to the global object.
windowundefined (Strict mode prevents accidental global variable creation).JavaScript
function showThis() {
console.log(this);
}
showThis(); // window (non-strict) or undefined (strict)
When a function is called as a method of an object, this points to the object that is standing before the dot.
JavaScript
const user = {
name: "Gemini",
greet: function() {
console.log(`Hello, I am ${this.name}`);
}
};
user.greet(); // "this" is 'user'. Output: "Hello, I am Gemini"
Warning: If you copy the method to a variable and call it, you lose the binding!
JavaScript
const looseGreet = user.greet; looseGreet(); // Error or undefined! "this" is now back to Default Binding.
What if you want to force this to point to a specific object? JavaScript gives us three tools:
.call()Invokes the function immediately and passes arguments one by one.
.apply()Invokes the function immediately and passes arguments as an array.
.bind()Does not invoke the function. Instead, it returns a new function with this permanently locked to the object.
JavaScript
const person1 = { name: "Alice" };
const person2 = { name: "Bob" };
function introduce(skill, city) {
console.log(`${this.name} knows ${skill} and lives in ${city}`);
}
introduce.call(person1, "JavaScript", "New York");
introduce.apply(person2, ["Python", "London"]);
const bobIntro = introduce.bind(person2, "Cooking", "Paris");
bobIntro();
When a function is called with the new keyword, this refers to the brand new object being created.
JavaScript
function Car(model) {
this.model = model;
}
const myCar = new Car("Tesla");
console.log(myCar.model); // "Tesla"
Arrow functions do not have their own this. They inherit this from their surrounding (lexical) scope. This makes them perfect for callbacks and timers where you don’t want the context to change.
JavaScript
const group = {
title: "Developers",
members: ["Alice", "Bob"],
showMembers() {
// Regular function inside forEach would lose "this"
// Arrow function inherits "this" from showMembers()
this.members.forEach((member) => {
console.log(`${this.title}: ${member}`);
});
}
};
group.showMembers();
Question: What will the following code output?
JavaScript
const obj = {
msg: "Secret",
print: function() {
setTimeout(function() {
console.log(this.msg);
}, 100);
}
};
obj.print();
Answer: undefined. Why? setTimeout is a Web API that calls the inner function separately. By the time it runs, the “Implicit Binding” to obj is lost, and it falls back to “Default Binding” (the window). The Fix: Use an Arrow Function inside setTimeout, or use .bind(this).
In many bugs and interview questions, one word appears again and again:
👉 this
Understanding this is about understanding:
this?this is a special reference variable that points to the current execution context.
In simple terms:
thisrefers to the object that is currently executing the code.
⚠️ The value of this is not fixed — it depends on how a function is called, not where it is written.
Context means the environment in which code is executed.
It defines:
thisthis in Global Contextthis refers to windowthis refers to an empty object {}Example:
this === window // true (browser)
thisthis exists only inside class methodsthis Inside a FunctionIn JavaScript:
this → undefined (strict mode)this → global object (non-strict)Example:
function show() {
console.log(this)
}
show()
Function call decides
this, not function definition
this Inside an Object (Method Context)When a function is called as a method of an object, this refers to that object.
Example:
user.printName()
Here:
this → user
this Inside a ConstructorWhen using new keyword:
this refers to the newly created objectExample:
new User("Prakash")
this → new User instance
this in Arrow FunctionsArrow functions do not have their own this.
They inherit this from their lexical (outer) scope.
Example:
() => {
console.log(this)
}
Useful for:
this in Event Handlersthis → element that triggered the event
this → outer scope
This is a very common interview question.
thisJavaScript allows manual control of this.
call()Calls function with specified this.
apply()Same as call, but arguments passed as array.
bind()Returns a new function with permanently bound this.
this in Class-Based Languagesthis refers to current objectExample use cases:
this vs self (Python)selfthisExample:
self.name = name
this Pitfallsthis behaves same in all languages