Skip to content
FunctionsChapter 17 / 47Intermediate5 min read

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 thisWith this
A separate function for every objectOne method that works on every object
The object's name is hardcodedthis tells you the current object by itself
Accessing class instance data is hardthis gives each instance its own data
Copy and pasted codeReusable methods and classes

3. Where is it used?

AreaWhat this is
Object methodsThe object the method was called on
ClassesThe newly created instance
DOM event listener (normal function)The element the listener is attached to
Callbacks with arrowThe this of the outer function
Library codeA this you set yourself with call, apply, bind

4. How does it work?

The rules for this, in order of priority:

RuleShape of the callthis
new bindingnew User()The new object
Explicitfn.call(obj), fn.bind(obj)obj
Implicitobj.fn()The obj before the dot
Defaultfn()undefined in strict mode
Arrow() => thisThe this of the outer scope
How to find the value of this
Is it an arrow function? Then the outer this
Was it called with new? Then the new object
Was call, apply or bind used? Then the given object
Does it look like obj.method()? Then obj
Otherwise undefined in strict mode

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

js
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

js
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?

ItemMeaning
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 thisMakes method chaining possible
Arrow in reduceIt 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

Suggested order
Objects
The this Keyword
call, apply & bind
Classes

Final quick revision

PointDetail
When this is decidedAt call time
obj.fn()this = obj
new Fn()this = the new object
call / apply / bindSet this yourself
Plain fn() in strict modeundefined
Arrow functionThe outer this