this Keyword
“this” is decided by HOW a function is called, not where it is written. Once you remember that rule, this stops being mysterious.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 27, 2026
Related tutorials
Learning objectives#
- Determine this for method calls
- Recognise when this is lost
- Understand arrow functions and this
- Fix lost this with bind
Explanation#
Method call: in user.greet(), this is the object before the dot — user.
Plain call: if you copy the method into a variable and call greet(), there is no object before the dot. In strict mode this is undefined.
Arrow functions do not have their own this; they use the this of the surrounding code. That makes them ideal for callbacks inside methods.
bind creates a new function with this permanently fixed.
Code example: Who is “this”?#
'use strict';
const user = {
name: 'Asha',
greet() { return `Hi, I am ${this.name}`; },
greetLater() {
setTimeout(() => console.log('Arrow keeps this:', this.name), 0);
},
};
console.log(user.greet());
const greet = user.greet; // copied without the object
try { greet(); } catch (e) { console.log('Lost this:', e.message); }
const bound = user.greet.bind(user);
console.log(bound());
user.greetLater();
Hi, I am Asha Lost this: Cannot read properties of undefined (reading 'name') Hi, I am Asha Arrow keeps this: Asha
Step-by-step explanation#
- user.greet() has user before the dot, so this.name is "Asha".
- greet() has nothing before the dot; in strict mode this is undefined, so reading .name throws.
- bind(user) returns a function whose this is always user.
- The arrow inside setTimeout uses greetLater’s this, which is user.
Real-world use case#
Passing object methods as event handlers or callbacks (button.addEventListener('click', obj.handle)) loses this — a very common bug fixed with bind or arrow functions.
Common mistakes#
setTimeout(user.greet, 0) calls greet without user. Use () => user.greet() or user.greet.bind(user).
An arrow function method cannot see the object as this.
Best practices#
- Use method shorthand for object methods
- Use arrow functions for callbacks inside methods
- Use bind (or class field arrows) when passing methods around
- Enable strict mode (ES modules and classes are strict automatically)
Remember this#
this = whoever is before the dot when the function is CALLED.
No dot → no this (undefined in strict mode).
Arrows don’t have their own this — they borrow it.
Practice exercise#
Your task: Fix this bug so it prints "Counter: 1": const c = { n: 0, inc() { this.n++; console.log('Counter:', this.n); } }; setTimeout(c.inc, 0);
Show solution
const c = { n: 0, inc() { this.n++; console.log('Counter:', this.n); } };
setTimeout(() => c.inc(), 0); // or setTimeout(c.inc.bind(c), 0)
Mini quiz#
In obj.method(), what is this?
Want more? Practise all JavaScript MCQs · JavaScript interview questions
My notes#
Saved in this browser.