The keyword this in JavaScript is a special reference that points to the context in which a function is executed. Understanding what this refers to is crucial for writing predictable and bug-free code.

💡 Core Concept
this is determined at runtime depending on how a function is called, not where it is defined.
Common Contexts of this
- Global Context: In the global scope (outside any function),
thisrefers to the global object (windowin browsers). - Function Context: In a regular function call,
thisisundefinedin strict mode, or the global object otherwise. - Object Method: When a function is called as a method of an object,
thisrefers to that object. - Constructor Function: When called with
new,thisrefers to the newly created object. - Arrow Functions: Arrow functions do NOT have their own
this; they inheritthisfrom the surrounding lexical scope.
this Behavior| Invocation Type | Value of this |
|---|---|
| Global scope | Global object (window in browsers) |
| Regular function call | undefined (strict mode) or global object (non-strict) |
| Method call (object.function()) | The object owning the method |
Constructor (called with new) | Newly created object |
| Arrow function | Lexical this from enclosing scope |
Why does this matter?
It enables functions to operate in the context of different objects without rewriting them. However, misunderstanding this leads to bugs when this doesn't point where expected.
📌 Deep Dive: this in Method Calls vs. Regular Functions
const obj = {
name: 'Alice',
greet() {
console.log(this.name);
}
};
obj.greet(); // Alice
const greetFunc = obj.greet;
greetFunc(); // undefined (or error in strict mode)
undefined
⚠️ Common Pitfall
Assigning a method to a variable and calling it loses the original this binding. Use bind() or arrow functions to preserve context.
Using bind(), call(), and apply()
These methods can explicitly set the value of this:
bind(): returns a new function permanently bound to a specificthis.call()andapply(): immediately invoke the function with a giventhis.
📌 Deep Dive: Using bind()
const obj = { name: 'Bob' };
function sayName() {
console.log(this.name);
}
const boundSayName = sayName.bind(obj);
boundSayName(); // Bob
💡 Arrow Functions and this
Arrow functions do not have their own this. They inherit it from the surrounding code, which is useful to avoid losing context inside callbacks.
📌 Deep Dive: Arrow Function Inheriting this
const person = {
name: 'Eve',
greet: function() {
const inner = () => {
console.log(this.name);
};
inner();
}
};
person.greet(); // Eve
⚠️ Important
Don't confuse arrow functions with methods if you rely on this. Arrow functions as methods do NOT bind this to the object.
📌 Deep Dive: Arrow Function as Method
const obj = {
name: 'Chris',
greet: () => {
console.log(this.name);
}
};
obj.greet(); // undefined (or global object's name)
Summary
thisdepends on how a function is called, not where it is defined.- Method calls bind
thisto the owning object. - Regular functions have
thisasundefinedin strict mode. - Arrow functions inherit
thislexically from their surrounding scope.
Quick Knowledge Check
Test what you just learned
Question 1 of 2
What does this refer to inside a regular object method?
Question 2 of 2
What is special about this inside an arrow function?
Loading results...