this Keyword — Complete Guide
this Keyword — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of JavaScript Tutorial on Toolliyo Academy.
On this page
JavaScript Tutorial · Lesson 22 of 100
this Keyword
Basics ✓ → Objects & data → Async & DOM → Advanced → Tools → Projects
Intermediate · 2 — Built-in types & objects · ~6 min · JS Objects & Collections
What is this?
this refers to the object that "owns" the current function call. Its value depends on how the function was called, not where it was written.
Why should you care?
this shows up in classes, event handlers, and object methods — confusing this is a top beginner bug.
See it live — copy this example
Paste into an HTML file or the browser console (F12). Use Run below when the live editor is available.
const person = {
name: "Lee",
sayHi() {
console.log("Hi, " + this.name);
}
};
person.sayHi();
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- When you call person.sayHi(), this inside sayHi is person.
- If you copy sayHi to another variable, this may change.
Practice next
- Run person.sayHi() and see correct name.
- Copy sayHi to const fn = person.sayHi; fn() and see broken this.
- Fix with fn.call(person) or bind.
- Use call to invoke greet with a different object as this.
- Add an event listener using a regular function and log this — it is the element.
Remember
this depends on call site Methods: this is the object before the dot arrow functions inherit this from outer scope
ScriptVerse button handlers
A class-based widget relies on this pointing to the instance when handleClick runs from addEventListener.
Outcome: Correct this binding prevents methods from reading undefined instance data.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!