this
In one line
`this` is a keyword that refers to the object that is currently executing the code — its value depends on how the function is called.
In simple words
this is a special keyword whose value depends on how the current function was called. It is not fixed — it changes based on the calling context.
In a regular function call, this is undefined (strict mode) or the global object. In a method call (obj.method()), this is obj. In a constructor call (new Foo()), this is the new instance. Arrow functions do not have their own this — they inherit it from the enclosing scope.
You can explicitly set this with call, apply, or bind. bind creates a new function with this permanently fixed. This is useful for event handlers and callbacks where this would otherwise be lost.
Example
const obj = {
name: "Ada",
greet() { return this.name; }
};
console.log(obj.greet()); // "Ada"
const fn = obj.greet;
console.log(fn()); // undefined — this is lostthis is obj when called as obj.greet(), but lost when extracted and called as fn().
Common confusions
Confused with: arrow function this
The difference: Regular functions have their own this determined by the call; arrow functions inherit this from the enclosing lexical scope and cannot be changed with bind.
Related terms
Related Resources
Related Lessons
- Objects
Learn about this.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.