← All tutorials

GoCareerGo Tutorials

JavaScript Tutorial

Variables to closures, promises, the DOM and OOP — a complete JS reference.

Understanding this

this refers to the object that is executing the current function — and its value depends on HOW the function was called, not where it was defined.

  • Plain function call → this is undefined (strict mode) or the global object
  • Method call (obj.method()) → this is the object before the dot
  • Arrow functions → this is inherited lexically from the enclosing scope
  • call() / apply() / bind() → this is set explicitly
  • new Constructor() → this is the newly created object
const obj = {
  name: 'Ada',
  regular() { return this.name; },      // 'Ada' — this = obj
  arrow: () => { return this.name; },   // undefined — this = outer scope
};
Tip

Say the rule as 'this is determined by the call site, except for arrow functions which use lexical this'.