JavaScript Reference ยท Operators
The this keyword
Reference for the JavaScript this keyword: method calls, plain functions, strict mode, arrow functions, classes, call/apply/bind, events and constructors.
this is a keyword that evaluates to a value chosen when a function is called, not where it is written (arrow functions are the exception). In a method call it is the object before the dot; in a constructor call it is the new object; in a plain call it is undefined in strict mode and the global object otherwise. You can also set it explicitly with call(), apply() and bind().
Because the rules depend on the call, copying a method into a variable or passing it as a callback is the classic way to lose this.
obj.method(); // this === obj fn(); // this === undefined (strict) or globalThis (sloppy) new Ctor(); // this is the new object
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
this
Return value: depends on the context, summarized below.
| Context | Value of this |
|---|---|
Method call obj.f() |
obj (the receiver). |
Plain function call f() |
undefined in strict mode; globalThis in sloppy mode. |
new F() |
The newly created object (or the one F returns, if it returns an object). |
f.call(x), f.apply(x) |
x (in sloppy mode primitives are boxed and null/undefined become globalThis). |
f.bind(x) |
A new function whose this is permanently x. |
| Arrow function | The this of the enclosing scope at the time the arrow was created. |
| Class constructor, method, static method | The instance (constructor, methods) or the class itself (static). Class bodies are always strict. |
| Getter or setter | The object the property was accessed on, not necessarily the one that defines it. |
| Top level of a classic script | globalThis (window in browsers). |
| Top level of an ES module | undefined. |
DOM event handler (addEventListener with a regular function) |
The element the listener is attached to (event.currentTarget). Arrow handlers do not get this. |
Inline attribute handler (onclick="...") |
The element. |
Exceptions: in a derived class constructor, accessing this before calling super() throws a ReferenceError.
Usage notes
- Detached methods lose
this.setTimeout(obj.method, 100)andconst f = obj.method; f()call the method as a plain function. Use an arrow wrapper,bind()or an arrow class field. - Arrow functions are good for callbacks inside methods, but poor as the methods themselves.
- Strict mode makes mistakes visible:
this.x = 1in a detached call throws instead of silently creating a global. bind()cannot be overridden by a latercall(), and an arrow function ignorescall,applyandbindforthis.- Nested functions do not inherit
thisfrom the enclosing method unless they are arrows. - Optional chaining and
this.obj.method?.()keepsobjasthis;(0, obj.method)()does not. - Prefer explicit parameters over reliance on
thiswhen designing small utilities.
Browser support
this is a core part of the language and works the same in every engine. Module top-level this being undefined applies to environments that support ES modules.