How Does 'this' Keyword Work in JavaScript? A Beginner's Guide
Learn how JavaScript determines this from a function call, with examples of object methods, strict mode, arrow functions, and call or bind.

How Does this Work in JavaScript?
If you see this.name inside a function, how do you know which object's name it refers to? The answer usually comes from how the function is called, not where the function was written. Arrow functions are the main exception: they don't have their own this and inherit it from the surrounding scope.
We'll look at the common call patterns and a browser event handler. Run the JavaScript examples in a modern browser console or recent Node.js. For code that relies on strict-mode behavior, the example declares strict mode explicitly.
A method call: this is the object before the dot
When a regular function is called as an object's method, this refers to the object used for that call:
const user = {
name: "Mina",
sayName() {
console.log(this.name);
},
};
user.sayName(); // Mina
The function is called as user.sayName(), so this is user. It isn't permanently attached to the object where the method was defined; the call form matters.
A plain function call
For a plain call such as showThis(), there is no object before a dot to use as the receiver. In strict mode, this is undefined:
function showThis() {
"use strict";
console.log(this);
}
showThis(); // undefined
In a non-strict classic script, a plain call of a regular function substitutes the global object for this. The global object varies by environment, so use globalThis when you intentionally need to refer to it. ES modules are strict by default. Avoid relying on the non-strict substitution in modern code.
A detached method loses its receiver
If you take a method out of an object and call it as a plain function, it no longer receives that object as this:
const user = {
name: "Mina",
sayName() {
"use strict";
console.log(this.name);
},
};
const sayName = user.sayName;
sayName(); // TypeError: this is undefined in strict mode
The function was originally stored on user, but sayName() is now a plain call. Keep the method call as user.sayName(), or explicitly bind the receiver if you need to pass the method elsewhere.
Set this with call, apply, or bind
Regular functions let you provide a this value explicitly. call and apply call the function immediately; bind returns a new function with this fixed to the value you provide.
function introduce() {
console.log(`I'm ${this.name}.`);
}
const user = { name: "Mina" };
introduce.call(user); // I'm Mina.
introduce.apply(user); // I'm Mina.
const introduceMina = introduce.bind(user);
introduceMina(); // I'm Mina.
For call and apply, the first argument is the this value. apply takes any additional function arguments as an array; call takes them one by one.
Arrow functions inherit this
An arrow function doesn't create its own this. It uses the surrounding function's this, which can be helpful when a callback runs later:
const timer = {
seconds: 0,
start() {
setTimeout(() => {
this.seconds += 1;
console.log(this.seconds);
}, 300);
},
};
timer.start(); // 1, after the timer callback runs
Here, start is called as timer.start(), so its this is timer. The arrow callback inherits that same this. If you replaced the arrow with a regular callback function, its this would depend on how the timer API calls it.
An arrow doesn't become an object's method receiver just because it is stored on an object. If a method needs this to refer to the object used in the call, use method shorthand or a regular function.
this in a browser event handler
When the browser calls a regular function registered as an event listener, this refers to the element handling the event. An arrow instead inherits this from its surrounding scope:
<button id="save-button">Save</button>
<script>
const button = document.querySelector("#save-button");
button.addEventListener("click", function (event) {
console.log(this === event.currentTarget); // true
});
button.addEventListener("click", (event) => {
console.log(event.currentTarget === button); // true
// The arrow's `this` is inherited from the surrounding scope.
});
</script>
Use a regular function if you want the event target element as this. With an arrow, use event.currentTarget explicitly when you need the element.
Common beginner mistakes
Thinking this means the object where a function was defined
For regular functions, the call expression usually determines this. A method can lose its receiver when detached and called on its own.
Using an arrow for a method that needs its receiver
Incorrect for this purpose:
const counter = {
count: 0,
increment: () => {
this.count += 1;
},
};
Correct:
const counter = {
count: 0,
increment() {
this.count += 1;
},
};
The arrow inherits this from outside the object. The method shorthand receives the object when called as counter.increment().
Assuming a plain call always sets this to the global object
That substitution applies to a regular function called plainly in a non-strict classic script. In strict mode, including ES modules, this is undefined for a plain call.
Practice
Create an object with a name and a regular sayName method. Call it as a method, detach it and call it plainly in strict mode, then use .bind() to make a reusable function that keeps the object as this. Finally, add an arrow callback inside another method and see what it inherits.
What we learned
- For a regular function,
thisusually depends on how it is called. - In an object method call, the object before the dot is the receiver for
this. - A plain call gives
undefinedin strict mode; a non-strict classic script substitutes the global object. call,apply, andbindcan setthisfor regular functions.- Arrow functions inherit
thisfrom their surrounding scope. - Browser event handlers called as regular functions receive the event element as
this; arrows do not.
What's next?
Next, we'll learn what a prototype is and how JavaScript objects can share behavior through the prototype chain.
Conclusion
When this surprises you, look at the call site. Was the function called as an object method, as a plain function, or by the browser as an event handler? Then check whether it's an arrow, which inherits this, or a regular function, whose this follows the call. That simple check solves many common this bugs.
Written by
Dev Likhon
Full Stack Developer & Tech Educator
6+ years of professional development experience.
About Me
Comments
Share what you learned or ask a question.
Loading comments...