JavaScript: this

JavaScript this refers to a value chosen by the way a function is called.

What you will learn

Method context

const user = {
  name: "Aki",
  greet() {
    console.log(this.name);
  }
};

user.greet(); // Aki

When JavaScript evaluates user.greet(), this inside greet refers to user. Calling the same function without its object can produce a different value.

Other common contexts

Common mistake

Do not decide what this means from where a function is written. First inspect how it is called, then check whether it is an arrow function, a method, or a constructor.