this (JavaScript)

this is a keyword that refers to the object a function is currently running on. Its value depends on how the function is called.

JavaScript

Inside an object method, this is the object before the dot. In a regular standalone function it is undefined in strict mode (or the global object otherwise).

Arrow functions do not have their own this — they inherit it from the surrounding code, which makes them handy inside methods and callbacks.

Examples

JavaScript

const person = {
  name: "Lee",
  greet() {
    return "Hi, I'm " + this.name;
  }
};
console.log(person.greet());

Output:

Hi, I'm Lee

See also

Back to the Dictionary