Back to JavaScript Notes
Topic #310

The this Keyword

By the end of this lesson, you will be able to explain how JavaScript chooses the value of this and use it safely in object methods and callbacks.

What it is

this is a keyword that refers to the object currently associated with a function call. In a normal method, it is the object before the dot: obj.method() makes this equal to obj. In a constructor or class method called on an instance, it refers to that instance. It is not a fixed property of the function; it is chosen when the function is called. Related terms include implicit binding, explicit binding, default binding, and lexical this in arrow functions.

Why it matters

  • It lets methods read and update the object they belong to.
  • It helps write reusable object-oriented code without passing the object manually.
  • It explains common bugs when methods are detached or used as callbacks.
  • It guides when to use arrow functions, bind(), call(), or apply().
  • It is central to prototypes, classes, and many DOM event patterns.

Syntax or steps

  1. Define a method on an object using shorthand: name() { ... }.
  2. Call it as a method: obj.name().
  3. Inside the method, use this to access object properties.
  4. If the function may be detached, bind it: obj.name.bind(obj).
  5. For callbacks inside methods, prefer an arrow function so it inherits the method's this.
  6. Use call() or apply() when you need one-time explicit binding.

Example

const user = {
  name: "Ada",
  greet() {
    return "Hello, " + this.name;
  },
  announce() {
    console.log(this.greet());
  }
};

user.announce();

const detached = user.greet.bind(user);
console.log(detached());

const timerUser = {
  name: "Grace",
  start() {
    setTimeout(() => {
      console.log("Timer says: " + this.name);
    }, 0);
  }
};

timerUser.start();

Part by part: user.announce() calls greet() as a method, so this is user. bind(user) creates a new function whose this is fixed to user, even if called alone. The arrow function inside setTimeout does not create its own this; it inherits this from start(), which is timerUser. Expected output: Hello, Ada, Hello, Ada, Timer says: Grace.

Common mistakes

  • Losing the object: const fn = obj.method; fn(); changes this. Fix: call obj.method() or use bind().
  • Using a regular callback: setTimeout(function() { this.name; }, 0) does not keep the method's this. Fix: use an arrow function.
  • Assuming arrow functions have their own this: they use lexical this, so they are not good replacements for object methods that need dynamic binding.
  • Forgetting strict mode: in strict mode, default binding can make this undefined instead of the global object.

When to use it

PatternHow this is chosenUse when
obj.method()The object before the dotNormal object methods
arrow() inside a methodInherited from surrounding methodCallbacks like setTimeout
method.bind(obj)Explicitly fixed to objPassing a method as a callback
method.call(obj)Explicitly set for one callInvoking a function with a chosen object once

Practice

Guided exercise: Create an object called book with a title property and a describe() method that returns "Title: " + this.title. Call book.describe(). Expected output: Title: ....

Challenge: Fix this code so it logs Desk lamp is on:

const lamp = {
  name: "Desk lamp",
  turnOn() {
    setTimeout(function() {
      console.log(this.name + " is on");
    }, 0);
  }
};

lamp.turnOn();

Solution hint: replace function() { ... } with () => { ... }, or use bind(this).

Quick check

Question: In obj.method(), what is this inside method?

Answer: It is obj, because the method was called on that object.

Summary

this is about how a function is called, not where it is written. Use method calls for normal object behavior, arrow functions for callbacks that should inherit the method's context, and explicit binding when a function must keep a chosen object.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

The this Keyword – FAQs

Quick answers about learning The this Keyword in JavaScript.

This free note from Coding Now Tech Institute explains The this Keyword in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including The this Keyword, is 100% free with no signup required.
With focused practice, most students grasp The this Keyword in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now