Skip to content
YouTurkey Tech
All JavaScript topics

The this Keyword

Intermediate

The four rules that decide what this points to in a call (default, implicit, explicit and new), and why a method loses its object when you pass it around.Builds on Execution Context, Primitive vs Reference Types

Call stack

Empty

Scopes

Empty

Heap

Empty

Console

Empty

0/17Press Play, or step through with the arrows, to begin.
Step 0 of 17

Explanation

2 min read

On this page

What is the this keyword in JavaScript?

this is a value every regular function call receives, next to its arguments. Unlike variables, which are found through the scope where the code was written, this is decided by how the function is called. The same function can see a different this on every call.

Arrow functions are the exception: they have no this of their own and use the this of the code around them.

How does JavaScript decide what this is?

Look at the call and check four rules, strongest first:

  1. new: new User() creates an empty object, and this is that object.
  2. Explicit: fn.call(obj), fn.apply(obj) and fn.bind(obj) set this to obj. A function made with bind keeps that this for good; only new can override it.
  3. Implicit: obj.fn() sets this to the object right before the dot.
  4. Default: a plain call fn() gets undefined in strict mode, modules and classes. In old sloppy-mode scripts it gets the global object (window) instead.

Event listeners add one more case: in element.addEventListener('click', fn), a regular fn gets the element as this.

What mistakes do people make with this?

Mini example

function whoAmI() {  return this.id;} const a = { id: 'a', whoAmI };const b = { id: 'b' };const bound = whoAmI.bind(b); console.log(a.whoAmI()); // 'a': implicitconsole.log(a.whoAmI.call(b)); // 'b': explicit beats implicitconsole.log(bound.call(a)); // 'b': bind is permanent

Quick check

In the mini example, what does bound.call(a) return?

Further reading

All steps as text
  1. Step 1:

    The script runs in strict mode, as modules and classes always do. The global context starts.

  2. Step 2:

    Creation phase: greet and User are stored as functions, and user, admin and loose are reserved.

    Lines 3–18

  3. Step 3:

    Two objects are created. user gets a greet property pointing to the same function; admin has none.

    Lines 7–8

  4. Step 4:

    user.greet looks up greet on the user object and finds it there.

    Line 10

  5. Step 5:

    Rule 1, implicit binding: the function is called through user., so this is the object before the dot.

    Line 10

  6. Step 6:

    this.name reads name from the user object.

    Line 4

  7. Step 7:

    greet returns, and Hi, Ali is printed.

    Lines 4–10

  8. Step 8:

    Rule 2, explicit binding: call runs the same function with this set to the object you pass, here admin.

    Line 11

  9. Step 9:

    this.name now reads from admin, so Hi, Sara is printed. Same function, different this.

    Lines 4–11

  10. Step 10:

    Rule 3, new: it creates an empty object and calls User with this set to that new object.

    Line 16

  11. Step 11:

    this.name = name writes onto the new object.

    Line 14

  12. Step 12:

    new returns the new object, and .name reads Omar from it.

    Line 16

  13. Step 13:

    The line is done, and nothing holds on to the object, so it is unreachable and can be freed.

    Line 16

  14. Step 14:

    loose copies the function, not the object. The link to user is gone.

    Line 18

  15. Step 15:

    Rule 4, default binding: loose() has no object before the dot, so in strict mode this is undefined.

    Line 19

  16. Step 16:

    Reading name from undefined throws a TypeError, and the uncaught error ends the script.

    Line 4

  17. Step 17:

    this is decided by how a function is called, not where it is written. Check the call: new, then call/apply/bind, then an object before the dot, else undefined.