Easy'this' Keyword📖 Theory Question

How does 'this' work in different contexts?

💡

Hint

Determined by how a function is called, not where it is defined

Full Answer

this is determined by how a function is called:

  • Global → window / undefined (strict)
  • Method call → the object before the dot
  • new → the newly created object
  • call/apply/bind → whatever you pass
  • Arrow function → outer lexical scope
  • Event listener → the element that fired
const obj = { val: 42, getVal() { return this.val; } };
const fn = obj.getVal;
fn();          // undefined — lost context!
fn.call(obj);  // 42 — restored

More 'this' Keyword Questions

EasyExplain the four rules of this binding: default, implicit, explicit, and new.

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint