The this Keyword
Learn how the JavaScript this keyword gets its value, with the rules for method calls, new, call/bind and arrow functions, and simple examples.
On this page
1. What is the this keyword?
In simple words, this asks the function: "who called me?" If you write user.greet(), then this is user. Call the same function in some other way and this changes.
Only arrow functions are outside this rule. Their this is decided by the place where they are written.
2. Why does it matter?
Because of this, a single method can work with many objects. But if you do not understand the rules, you get undefined errors.
| Without this | With this |
|---|---|
| A separate function for every object | One method that works on every object |
| The object's name is hardcoded | this tells you the current object by itself |
| Accessing class instance data is hard | this gives each instance its own data |
| Copy and pasted code | Reusable methods and classes |
3. Where is it used?
| Area | What this is |
|---|---|
| Object methods | The object the method was called on |
| Classes | The newly created instance |
| DOM event listener (normal function) | The element the listener is attached to |
| Callbacks with arrow | The this of the outer function |
| Library code | A this you set yourself with call, apply, bind |
4. How does it work?
The rules for this, in order of priority:
| Rule | Shape of the call | this |
|---|---|---|
| new binding | new User() | The new object |
| Explicit | fn.call(obj), fn.bind(obj) | obj |
| Implicit | obj.fn() | The obj before the dot |
| Default | fn() | undefined in strict mode |
| Arrow | () => this | The this of the outer scope |
Modules and classes always run in strict mode, so in modern code a plain fn() call has this as undefined.
5. Code example
Implicit binding and a lost this
const user = {
name: "Ayesha",
greet() {
return `Hi, I am ${this.name}`;
},
};
console.log(user.greet());
const greetFn = user.greet;
try {
console.log(greetFn());
} catch (error) {
console.log(error.message);
}
const bound = user.greet.bind(user);
console.log(bound());Class and arrow callback
class Cart {
items = [];
add(item) {
this.items.push(item);
return this;
}
total() {
return this.items.reduce((sum, item) => sum + item.price, 0);
}
}
const cart = new Cart();
cart.add({ price: 300 }).add({ price: 200 });
console.log(cart.total());How does the code work?
| Item | Meaning |
|---|---|
user.greet() | Implicit binding, this = user |
greetFn() | The method came apart, this is undefined, an error is thrown (in a module) |
bind(user) | A new function whose this is always user |
new Cart() | this is the new cart instance |
return this | Makes method chaining possible |
Arrow in reduce | It has no this of its own, but it does not need one here |
Interview tip
this is decided at call time, not at definition time (except for arrows). In an interview, find the call site first when you look at code, then apply the priority list above.
6. Common mistakes
Mistake: Passing a method directly as a callback
Why? In setTimeout(user.greet, 100), the method is separated from the object and this is lost.
Better: Give an arrow wrapper, like setTimeout(() => user.greet(), 100), or use user.greet.bind(user).
Mistake: Making an object method an arrow function
Why? An arrow's this is not the object but the outer scope, so this.name is undefined.
Better: Write method shorthand for methods: greet() and use this inside.
Mistake: Expecting this to be the element in an event listener arrow
Why? An arrow function's this is not the element, so this.classList does not work.
Better: Use event.currentTarget for the element, which is correct in both arrow and normal functions.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Functions, Arrow Functions, Objects |
| Alongside | call, apply & bind |
| Later | Classes, Prototypes & Inheritance |
Final quick revision
| Point | Detail |
|---|---|
When this is decided | At call time |
obj.fn() | this = obj |
new Fn() | this = the new object |
call / apply / bind | Set this yourself |
Plain fn() in strict mode | undefined |
| Arrow function | The outer this |