this

Refers to the execution context of the current function call.

Since ES1 Spec ↗

Syntax

this

Returns

any — The current context: the receiver of a method call, a new instance in a constructor, the bound value, or `undefined`/global otherwise.

Examples

const obj = {
  name: "Ada",
  greet() { return "Hi, " + this.name; },
};
console.log(obj.greet());
Output
Hi, Ada
function show() { return this; }
const bound = show.bind({ id: 1 });
console.log(bound());
Output
{ id: 1 }
const obj = {
  value: 42,
  get() {
    const arrow = () => this.value;
    return arrow();
  },
};
console.log(obj.get());
Output
42

Notes

- `this` is determined by how a function is called, not where it is defined (except for arrow functions, which capture it lexically). - In strict-mode standalone calls `this` is `undefined`. - `call`, `apply`, and `bind` set `this` explicitly.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also