LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
JavaScript

this Keyword in JavaScript Explained

The keyword this refers to the object connected to the current function call. It helps us access an object's own properties and methods. But here is something important: the value of this can change depending on how a function is called. Let's see how it works in object methods, regular functions and arrow functions.

JavaScript this Keyword Explained with Real Examples

Quick Answer: The JavaScript this keyword refers to the value associated with the current function call. In an object method, it usually refers to the object before the dot. Its value changes based on how a function is called, not simply where it is written.

1. Why Do We Need the this Keyword?

Imagine a bank application manages 1,000 customer accounts.

Each account has a customer name, account number and balance. Each account also needs a method to display its details.

How can one method read the correct account's information?

The problem without this

We can pass the account details into a separate function. This works, but every call must pass the correct object.

Example 1: Display bank accounts without this

const raviAccount = {
  name: "Ravi",
  accountNumber: "SB1001",
  balance: 25000
};

const priyaAccount = {
  name: "Priya",
  accountNumber: "SB1002",
  balance: 40000
};

function showAccount(account) {
  console.log(account.name);
  console.log(account.accountNumber);
  console.log(account.balance);
}

showAccount(raviAccount);
showAccount(priyaAccount);

Expected output

Ravi
SB1001
25000
Priya
SB1002
40000

This code works correctly. But the function needs the account passed every time.

What becomes difficult as the application grows?

  1. Every method call must receive the correct account.
  2. Account-related actions can become separated from account data.
  3. More parameters can make method calls harder to read.
  4. Reusable object methods need a clear way to access their owner.

Now think about hundreds of account operations. A method should work with the account that calls it.

The solution with this

We can define the method inside each account object. The this keyword accesses the calling object.

Example 2: Read account details using this

const raviAccount = {
  name: "Ravi",
  accountNumber: "SB1001",
  balance: 25000,

  showDetails() {
    console.log(this.name);
    console.log(this.accountNumber);
    console.log(this.balance);
  }
};

const priyaAccount = {
  name: "Priya",
  accountNumber: "SB1002",
  balance: 40000,

  showDetails() {
    console.log(this.name);
    console.log(this.accountNumber);
    console.log(this.balance);
  }
};

raviAccount.showDetails();
priyaAccount.showDetails();

Expected output

Ravi
SB1001
25000
Priya
SB1002
40000
Passing an object versus using this
Without this With this
Pass the account as an argument. Read the calling object directly.
The function receives account data. The method uses its call context.
Useful for standalone functions. Useful for object methods.

The idea to remember:

  • Object = the bank account and its data.
  • Method = an action defined inside the account object.
  • this = the value linked to the current function call.
  • Call site = the place where the function is called.

2. How Does this Work in JavaScript?

JavaScript decides the value of this when a regular function runs.

For a method call, look at the object before the dot. That object becomes this.

Animation 1: How JavaScript finds this

A draw.io-style execution flow for a bank account method call.

1. Method call raviAccount.showDetails()
2. Find the receiver The object before the dot is raviAccount
3. Set this this refers to raviAccount for this call
4. Read object properties this.name and this.balance
5. Display account details Ravi · SB1001 · 25000
Execution step Result

The animation highlights the method call, receiver, this binding and property access in order.

3. this Inside an Object Method

An object method is a function stored inside an object.

When you call it through that object, this refers to that object.

Example 3: Calculate a bank account balance

const bankAccount = {
  name: "Lakshmi",
  balance: 30000,

  deposit(amount) {
    this.balance += amount;
    console.log(this.balance);
  }
};

bankAccount.deposit(5000);

Expected output

35000

Understand the important lines

  • bankAccount.deposit(5000) calls the method.
  • this refers to bankAccount during this call.
  • this.balance reads the current balance.
  • += adds the deposit to the existing balance.
  • The updated balance is then printed.

4. this Inside a Regular Function

A regular function does not always get its value from an object.

In a browser's non-strict script, a standalone regular function usually gets the global object as this.

In strict mode, a standalone regular function gets undefined.

Example 4: this in strict mode

"use strict";

function checkThis() {
  console.log(this);
}

checkThis();

Expected output

undefined

Important: Browser scripts, ES modules and Node.js modules have different execution contexts. Do not assume that every standalone function gets the browser's global object.

5. this Inside a Constructor Function

A constructor function helps create multiple objects with the same structure.

When you call it using new, JavaScript creates an object. Inside the constructor, this refers to that new object.

Example 5: Create customer bank accounts

function BankAccount(name, balance) {
  this.name = name;
  this.balance = balance;

  this.showBalance = function() {
    console.log(this.name + ": " + this.balance);
  };
}

const ravi = new BankAccount("Ravi", 25000);
const priya = new BankAccount("Priya", 40000);

ravi.showBalance();
priya.showBalance();

Expected output

Ravi: 25000
Priya: 40000

What happens when new is used?

  1. JavaScript creates a new empty object.
  2. The constructor's this refers to that object.
  3. The constructor adds the supplied properties.
  4. The new object is returned automatically.

6. this Inside a JavaScript Class

JavaScript classes use this to access the current instance.

An instance = one object created from a class.

Example 6: BankAccount class

class BankAccount {
  constructor(name, balance) {
    this.name = name;
    this.balance = balance;
  }

  deposit(amount) {
    this.balance += amount;
  }

  showDetails() {
    console.log(this.name);
    console.log(this.balance);
  }
}

const account = new BankAccount("Arjun", 50000);

account.deposit(10000);
account.showDetails();

Expected output

Arjun
60000

Here, this refers to the current account instance. The constructor sets its values, and the methods use them.

7. this Inside Arrow Functions

Arrow functions handle this differently from regular functions.

An arrow function does not create its own this. It uses the value from its surrounding scope.

This is called lexical this = this taken from the surrounding code.

Regular function versus arrow function

Example 7: Compare regular and arrow functions

const bankAccount = {
  name: "Priya",

  regularMethod() {
    console.log(this.name);
  },

  arrowMethod: () => {
    console.log(this.name);
  }
};

bankAccount.regularMethod();
bankAccount.arrowMethod();

Typical browser classic-script output

Priya
undefined

Important: The second output depends on the surrounding scope. This browser example assumes a classic script with no global property named name. In modules and other environments, the result can differ.

The regular method gets this from the method call. The arrow function keeps the surrounding value instead.

Animation 2: Regular function versus arrow function

Two different paths show where each function gets this.

bankAccount method call bankAccount.someMethod()
Regular method

Uses the object before the dot as this.

Arrow function

Uses this from its surrounding lexical scope.

Object receiver this can refer to bankAccount
Outer this Not automatically bankAccount

The comparison highlights the key difference: regular functions receive this from the call, while arrows inherit it from their surrounding scope.

When should you use arrow functions?

  • Use arrow functions for callbacks that should keep the outer this.
  • Use regular methods when the method needs the object calling it.
  • Avoid arrow functions as object methods when you expect this to mean that object.

8. Changing this with call(), apply() and bind()

JavaScript provides three methods to control this for regular functions.

  • call() calls a function with a chosen this value and separate arguments.
  • apply() calls a function with a chosen this value and an argument array.
  • bind() returns a new function with a chosen this value.

Example 8: Use call, apply and bind

const raviAccount = { name: "Ravi" };
const priyaAccount = { name: "Priya" };

function showMessage(message, amount) {
  console.log(message + " " + this.name + ": " + amount);
}

showMessage.call(raviAccount, "Balance for", 25000);

showMessage.apply(priyaAccount, ["Balance for", 40000]);

const showRavi = showMessage.bind(raviAccount);
showRavi("Balance for", 25000);

Expected output

Balance for Ravi: 25000
Balance for Priya: 40000
Balance for Ravi: 25000
call, apply and bind compared
Method What it does Arguments
call() Calls the function immediately. Separate arguments.
apply() Calls the function immediately. One argument array.
bind() Returns a new function. Can preset arguments.

Remember: These methods control this for regular functions. They cannot replace the lexical this value inside an arrow function.

9. Common Mistakes with this

Mistake 1: Extracting a method from its object

When you store a method in another variable, the original receiver is not kept automatically.

Incorrect example

const account = {
  name: "Ravi",
  showName() {
    console.log(this.name);
  }
};

const show = account.showName;
show();

Expected output in strict mode

TypeError: Cannot read properties of undefined (reading 'name')

Correction: Keep the object call, or bind the method.

Corrected example

const show = account.showName.bind(account);
show();

Expected output

Ravi

Mistake 2: Using an arrow function as an object method

An arrow function does not get its own object-based this value.

Use method syntax when you need the calling object.

Mistake 3: Assuming this always means the current object

The value depends on the function type and call style. Check the call site before making assumptions.

Mistake 4: Forgetting strict mode

A standalone regular function gets undefined in strict mode. This can cause errors when code reads its properties.

10. Best Practices

  • Use method syntax for object methods that need the object as this.
  • Use arrow functions for callbacks that should inherit the outer this.
  • Use bind() when a method must keep a specific receiver.
  • Check how a function is called before debugging its this value.
  • Prefer clear parameters when a standalone function does not need object context.
  • Avoid changing this when a simple function parameter is easier to understand.

11. How this Comes Up in JavaScript Interviews

Question 1: What is this in JavaScript?

What the interviewer expects: Explain that this depends on the call style.

Strong answer: "The this keyword refers to the value linked to a function call. In an object method, it usually refers to the object before the dot. In an arrow function, it comes from the surrounding scope."

Question 2: What is the difference between regular and arrow functions?

Strong answer: "A regular function gets its this value when it is called. An arrow function does not create its own this. It uses the value from its surrounding scope."

Question 3: What is the difference between call, apply and bind?

Strong answer: "All three can set this for a regular function. Call accepts separate arguments. Apply accepts an array of arguments. Bind returns a new function for later use."

Question 4: What happens to this in a constructor?

Strong answer: "When we call a constructor with new, JavaScript creates an object. Inside the constructor, this refers to that new object. We use it to assign the object's properties."

What should different experience levels explain?

  • Beginner: Explain object methods and arrow function differences.
  • Working developer: Explain callbacks, bind and extracted methods.
  • Senior developer: Explain call-site behavior, strict mode and debugging.
  • Architect: Explain API design choices and when explicit parameters are clearer.

12. Hands-On Practice

Beginner: Create a customer account

Create an object with a name and balance. Add a method that prints both values using this.

Expected result: The method prints the values belonging to that account.

Intermediate: Add money to two accounts

Create two bank account objects. Add a deposit method to each object.

Call the method for both accounts. Check that each balance changes separately.

Advanced: Fix an extracted method

Store an account method in another variable. Call it and observe the result.

Fix the issue using bind(). Then compare it with an explicit parameter.

Extra challenge: Predict the output

Before running this code, predict the output of each function call.

Practice question

const account = {
  name: "Lakshmi",
  showName() {
    console.log(this.name);
  }
};

account.showName();

const show = account.showName.bind(account);
show();

Expected output

Lakshmi
Lakshmi

13. Frequently Asked Questions

Does this always refer to an object?

No. In strict mode, a standalone regular function receives undefined. Arrow functions inherit this from their surrounding scope.

Can an arrow function use this?

Yes. It can read this from its surrounding scope. It cannot create its own this value.

Can we change this in JavaScript?

For regular functions, call, apply and bind can set the this value. These methods do not change an arrow function's lexical this.

What does this mean inside a class method?

When called through an instance, this refers to that instance. Detached methods can lose their original receiver.

Is this the same as self in other languages?

Not exactly. JavaScript determines this from the function call, except for arrow functions. Other languages may follow different rules.

14. Next Steps and Community

You now understand how JavaScript decides the value of this. Practice with object methods, constructors, classes and callbacks.

Explore Further: Read the official MDN guide to the JavaScript this operator.

Share Your Knowledge: Share a tricky this example with the Upgradix community. Explain what the call site does and why the output appears.

Keep practising, and make JavaScript execution easier to understand.

Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.