Lesson

JS This Keyword

10 min reading
Free Course

JavaScript this Keyword: How Execution Context Works

The JavaScript this keyword points to the object that is running the current function or method. Unlike ordinary variables, which follow lexical scope, this is decided by how a function is called, except in arrow functions, which behave differently.

The Five this Binding Rules

flowchart TD
    Call["How was the function called?"] --> Arrow{"Arrow function?"}
    Arrow -- "Yes" --> Lex["Inherits outer lexical this"]
    Arrow -- "No" --> New{"Called with new?"}
    New -- "Yes" --> Inst["Points to new instance"]
    New -- "No" --> Expl{"call / apply / bind?"}
    Expl -- "Yes" --> Given["Points to passed object"]
    Expl -- "No" --> Method{"Called as obj.fn()?"}
    Method -- "Yes" --> Owner["Points to obj before dot"]
    Method -- "No" --> Plain["undefined (strict) or global"]

How the JavaScript this Keyword Gets Its Value

When you are unsure what this is, look at the call site, not the place where the function was written. Work down this list and stop at the first rule that matches:

How the function runs

What this is

It is an arrow function

Inherited from the surrounding scope where the arrow was written

Called with new

The newly created instance

Called with call(), apply(), or bind()

The object you pass in

Called as obj.fn()

obj, the object before the dot

Called as a plain fn()

undefined in strict mode, otherwise globalThis

Strict mode is on by default inside ES modules and class bodies, so rule 5 gives undefined in most modern code. The full behavior is documented in MDN's this reference. If functions themselves are still new to you, start with our lesson on JavaScript functions.

Losing this and Fixing It with bind() or Arrow Functions

Passing user.greet directly as a callback strips away user before the dot, making this equal undefined inside greet. Fix that by wrapping in an arrow function or calling .bind(user).

const tracker = {
  label: "Downloads",
  count: 0,
  increment() {
    this.count += 1;
    console.log(`${this.label}: ${this.count}`);
  },
};

tracker.increment(); // this === tracker

// Explicit binding with bind() preserves tracker context
const detachedIncrement = tracker.increment.bind(tracker);
detachedIncrement();

// call() invokes immediately with a custom this
function announce(prefix) {
console.log(${prefix} ${this.label} = ${this.count});
}
announce.call(tracker, "[Metric]");

Call(), apply(), and bind() at a Glance

  • call(ctx, a, b) runs the function now, with arguments listed one by one.

  • apply(ctx, [a, b]) runs it now, with arguments in an array.

  • bind(ctx) runs nothing yet. It returns a new function with this locked in.

Common Mistakes

Passing obj.method straight to a callback

setTimeout(obj.method, 100) or button.addEventListener("click", obj.method) hands over the function without obj. Bind it with obj.method.bind(obj) or wrap it as () => obj.method().

Using an arrow function as an object method

const user = { name: "Ali", say: () => this.name };

An arrow function does not get its own this. It uses the one from where the object literal sits, such as the module or global scope, not user. Depending on where the code runs, this is undefined or throws. Write say() { return this.name; } instead. Note that call(), apply(), and bind() cannot change an arrow function's this either.

Forgetting how DOM listeners treat this

button.addEventListener("click", function () {
  console.log(this === button); // true
});

button.addEventListener("click", () => {
console.log(this === button); // false, this comes from the outer scope
});

In a regular function listener, this is the element the listener is attached to, the same value as event.currentTarget. If you want code that works in both styles, read event.currentTarget instead of relying on this. The details are covered under addEventListener().

How to check which this you have

Add console.log(this) at the top of the function, or test this === tracker. If you see undefined, window, or the wrong object, trace back to how the function was called.

Frequently Asked Questions

What is the difference between call(), apply(), and bind()?

call() and apply() invoke the function immediately with this set to the object you pass. They differ only in how arguments are supplied. bind() returns a new bound function that you call later.

What is this inside a global function in strict mode?

It is undefined. This prevents a forgotten new or a detached method from silently editing global properties.

When should I use an arrow function instead of a regular one?

Use an arrow for callbacks inside methods, where you want to keep the outer this. Use a regular function or method shorthand for object methods and constructors.

Conclusion

To predict this, ask how the function was called. Arrows inherit it, new creates it, call/apply/bind set it, a dot supplies it, and a plain call leaves you with undefined or the global object. Once that habit sticks, move on to our lesson on JavaScript classes, where this shows up constantly.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
Lesson Recap Quiz Available

Test Your Knowledge

You've completed this section! Take a quick 5-question quiz to check your understanding.

JavaScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum