JavaScript this Keyword Explained: Every Binding Rule, Tested in Node
The JavaScript this keyword changes value depending on how a function is called, not where it is written. Learn the five rules, why this gets lost in callbacks, and how arrow functions, bind and classes fix it, with 17 cases we ran.
- The call site decides
this. The same function can see different values on different calls. - Copying a method into a variable or passing it as a callback loses its object; in strict code
thisbecomesundefined. - Arrow functions have no own
this. They use the value from the surrounding code, which fixes most callback bugs. bindlocksthispermanently. In our tests, a secondbindand a latercallwere both ignored.
Few things confuse JavaScript developers as often as the JavaScript this keyword. A method works in one place, then fails with “Cannot read properties of undefined” when you pass it to a timer or an event handler. The reason is simple once you see it: this is not fixed when the function is written. It is decided each time the function is called.
We ran 17 small cases in Node.js 24.22.1, mostly in a CommonJS file with "use strict" at the top, and the outputs below are copied from that run. Browser-only cases, such as DOM event handlers, are described but were not run. If scope and closures are new to you, read our JavaScript closures guide first, because arrow functions rely on the same idea.
What is this in JavaScript?
this is a special value available inside every regular function. Think of it as an extra, hidden argument that JavaScript fills in when you call the function. Most of the time it points to the object the function was called on. The rules below explain exactly how JavaScript chooses it.
Rule 1: method calls use the object before the dot
"use strict";
const user = { name: "Asha", greet() { return `Hi, ${this?.name}`; } };
console.log(user.greet()); // Hi, Asha
Because we called user.greet(), the object before the dot, user, became this. With nested objects, only the nearest object counts:
const nested = { name: "outer", a: { name: "inner", hi() { return this.name; } } };
console.log(nested.a.hi()); // inner
How this gets lost
Now copy the same method into a variable and call it on its own:
const detached = user.greet;
console.log(detached()); // Hi, undefined
There is no dot at the call site, so there is no object. We used optional chaining (this?.name) to show the value; without it, the call would throw a TypeError. This is exactly what happens when you pass user.greet as a callback: the receiving code calls it as a plain function and the object is gone.
Rule 2: default binding in plain function calls
function whoAmI() { return this; }
console.log(whoAmI()); // undefined (strict mode)
In strict mode, a plain call gives undefined. We also ran the same function in a non-strict file, and this === globalThis printed true. That old behaviour silently writes to global variables when you forget new or lose a method, which is one reason modern code is strict. ES modules and class bodies are always strict, so you get the safer undefined by default.
Rule 3: call, apply and bind set this explicitly
console.log(whoAmI.call({ id: 1 }), whoAmI.apply({ id: 2 }));
// { id: 1 } { id: 2 }
const bound = user.greet.bind({ name: "Ben" });
console.log(bound()); // Hi, Ben
console.log(bound.bind({ name: "X" })()); // Hi, Ben
console.log(bound.call({ name: "Y" })); // Hi, Ben
call and apply invoke the function once with the this you pass; they differ only in how extra arguments are given (a list for call, an array for apply). bind returns a new function whose this is fixed forever. Our test confirms the “forever”: binding again and calling with a different object both still printed Hi, Ben.
Some array methods accept a thisArg too:
console.log([1, 2].map(function (x) { return x * this.k; }, { k: 10 })); // [ 10, 20 ]
Arrow functions: this from the surrounding code
Arrow functions do not create their own this. They read it from the code around them at the moment they are created, just like any other outer variable.
const counter = {
count: 0,
startRegular() { return [1, 2, 3].map(function () { return this; }); },
startArrow() { return [1, 2, 3].map(() => this.count); },
arrowMethod: () => typeof this,
};
console.log(counter.startRegular()); // [ undefined, undefined, undefined ]
console.log(counter.startArrow()); // [ 0, 0, 0 ]
console.log(counter.arrowMethod()); // object (module.exports, not counter)
The regular callback lost this, while the arrow callback kept the method’s this and read count. The third line shows the opposite mistake: an arrow function used as a method does not see the object. In our CommonJS file it saw module.exports. Rule of thumb: use regular method syntax for object methods, and arrow functions for callbacks inside them.
Tip: call, apply and bind cannot change an arrow function’s this. If a library tries to set this for your callback, use a regular function instead.
Callbacks, timers and events
const obj = {
val: 42,
later() { setTimeout(function () { console.log(typeof this); }, 0); },
laterArrow() { setTimeout(() => console.log(this.val), 0); },
};
The regular function printed object, but that object was Node’s internal Timeout, not obj. The arrow version printed 42. In browsers, a regular timer callback gets the global window object instead; we did not run the browser case. Either way, the regular function does not see your object.
DOM event handlers follow their own rule: with element.addEventListener("click", function () { ... }), this is the element that received the event. With an arrow function it is whatever the surrounding code had. Both are valid; choose based on whether you need the element or your component. We did not run DOM examples here. Timers and async callbacks run later through the event loop, which our event loop guide explains.
Rule 4: new and classes
function Car(make) { this.make = make; }
const c = new Car("Tata");
console.log(c.make, c instanceof Car); // Tata true
new creates a fresh object, makes it this inside the function, and returns it. Classes work the same way, with a twist for detached methods:
class Timer {
seconds = 0;
tickArrow = () => ++this.seconds;
tickMethod() { return ++this.seconds; }
}
const t = new Timer();
const { tickArrow, tickMethod } = t;
tickArrow(); // 1
tickMethod(); // TypeError: Cannot read properties of undefined (reading 'seconds')
A class field holding an arrow function captures the instance, so it survives being detached. That is why some codebases write event handlers this way. The cost is memory: each instance gets its own copy of the function instead of sharing one on the prototype. For TypeScript users, the same rules apply, as covered in our TypeScript tutorial.
Top-level this: CommonJS vs ES modules
Outside any function, this depends on the module system. In our CommonJS file, this === module.exports printed true. In an .mjs ES module, top-level this printed undefined. In a classic browser script it is the window object. If you are moving code between formats, our ES modules vs CommonJS guide covers other differences that bite.
The rule order in one table
When several rules could apply, JavaScript checks them in this order:
| Call style | this is | Example result |
|---|---|---|
new Fn() |
The new object | Tata true |
fn.bind(obj) or call/apply |
The object you pass | Hi, Ben |
obj.fn() |
The object before the dot | Hi, Asha |
fn() |
undefined (strict) or global object |
undefined |
| Arrow function | Inherited from surrounding code | [ 0, 0, 0 ] |
Debugging this in practice
- Log it:
console.log(this)at the top of the function shows what you actually got. - Look at the call site, not the definition. Search for where the function is passed or invoked.
- If a method is passed as a callback, wrap it:
() => user.greet(), or bind it once in the constructor. - Prefer strict mode, so mistakes fail loudly with
undefinedinstead of touching globals. - In tests, call methods both directly and as callbacks; the second style catches lost
thisbefore users do.
Async code adds one more layer, because callbacks run after the current function has returned. Our JavaScript promises guide shows how then callbacks and async functions handle this, and the same arrow-function fix applies.
Final thoughts
The JavaScript this keyword stops being mysterious once you look at how a function is called. A dot gives the object, new gives a fresh object, bind, call and apply give whatever you pass, a plain call gives undefined in strict code, and arrow functions simply borrow from their surroundings. Learn those five lines, and most “undefined” errors become quick fixes.
Frequently asked questions
What does this refer to in JavaScript?
It depends on how the function is called. For obj.method() it is obj, with new it is the new object, with bind, call or apply it is the value you pass, and in a strict plain call it is undefined.
Why is this undefined in my callback?
Passing a method as a callback removes its object, so it is called as a plain function. In strict code that makes this undefined. Use an arrow function wrapper or bind the method.
Do arrow functions have their own this?
No. Arrow functions use this from the surrounding code where they were created, and call, apply or bind cannot change it. That makes them ideal for callbacks inside methods.
What is the difference between call, apply and bind?
call and apply run the function once with a chosen this, taking arguments as a list or an array. bind returns a new function with this fixed permanently; rebinding it had no effect in our test.
What is this at the top level of a file?
In a CommonJS file it is module.exports, in an ES module it is undefined, and in a classic browser script it is the global window object. We confirmed the first two in Node 24.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.