How Does the "this" Keyword Work in JavaScript?

"this" refers to the context an ordinary function was called with, which is determined by how the function is invoked, not where it's defined — a frequent so...

Published November 20, 2026

this refers to the object that is calling the function, decided at call time, not where the function is written. Arrow functions are the exception: they use the this of their surrounding scope.

The concept

Rules in order: new binds a new object; call/apply/bind set it explicitly; obj.method() sets this to obj; a plain call gives undefined (strict) or the global object.

Arrow functions capture this lexically, which makes them ideal for callbacks inside methods.

Who is calling?

obj.fn() this = obj
fn() this = undefined (strict)
fn.call(x) this = x
new Fn() this = new object
() => {} this from outer scope

Practical examples

Lost this

const user = { name: 'Ada', hi() { return this.name } }
const f = user.hi
f()   // undefined — detached from user

Fix with bind

const g = user.hi.bind(user)
g()   // 'Ada'

Arrow in a callback

const timer = {
  n: 0,
  start() { setInterval(() => { this.n++ }, 1000) }  // this = timer
}

React class handler (old style)

this.onClick = this.onClick.bind(this)

Common mistakes

  • Passing a method as a callback and losing this.
  • Using arrow functions as object methods when you need dynamic this.
  • Assuming this is the function itself.

Key takeaways

  • this = how the function is called.
  • Arrow functions inherit this.
  • Use bind or arrows to fix lost context.

FAQ

What is this at top level?

undefined in ES modules, the global object in scripts.

call vs apply vs bind?

call and apply invoke now (args list vs array); bind returns a new function.

More JavaScript articles