Cannibalized from another post of mine, here's more than you ever wanted to know about this.

Before I start, here's the most important thing to keep in mind about Javascript, and to repeat to yourself when it doesn't make sense. Javascript does not have classes (ES6 class is syntactic sugar). If something looks like a class, it's a clever trick. Javascript has objects and functions. (that's not 100% accurate, functions are just objects, but it can sometimes be helpful to think of them as separate things)

The this variable is attached to functions. Whenever you invoke a function, this is given a certain value, depending on how you invoke the function. This is often called the invocation pattern.

There are four ways to invoke functions in javascript. You can invoke the function as a method, as a function, as a constructor, and with apply.

As a Method

A method is a function that's attached to an object

var foo = {};
foo.someMethod = function(){
    alert(this);
}

When invoked as a method, this will be bound to the object the function/method is a part of. In this example, this will be bound to foo.

As A Function

If you have a stand alone function, the this variable will be bound to the "global" object, almost always the window object in the context of a browser.

 var foo = function(){
    alert(this);
 }
 foo();
 

This may be what's tripping you up, but don't feel bad. Many people consider this a bad design decision. Since a callback is invoked as a function and not as a method, that's why you're seeing what appears to be inconsistent behavior.

Many people get around the problem by doing something like, um, this

var foo = {};
foo.someMethod = function (){
    var that=this;
    function bar(){
        alert(that);
    }
}

You define a variable that which points to this. Closure (a topic all its own) keeps that around, so if you call bar as a callback, it still has a reference.

NOTE: In use strict mode if used as function, this is not bound to global. (It is undefined).

As a Constructor

You can also invoke a function as a constructor. Based on the naming convention you're using (TestObject) this also may be what you're doing and is what's tripping you up.

You invoke a function as a Constructor with the new keyword.

function Foo(){
    this.confusing = 'hell yeah';
}
var myObject = new Foo();

When invoked as a constructor, a new Object will be created, and this will be bound to that object. Again, if you have inner functions and they're used as callbacks, you'll be invoking them as functions, and this will be bound to the global object. Use that var that = this trick/pattern.

Some people think the constructor/new keyword was a bone thrown to Java/traditional OOP programmers as a way to create something similar to classes.

With the Apply Method

Finally, every function has a method (yes, functions are objects in Javascript) named "apply". Apply lets you determine what the value of this will be, and also lets you pass in an array of arguments. Here's a useless example.

function foo(a,b){
    alert(a);
    alert(b);
    alert(this);
}
var args = ['ah','be'];
foo.apply('omg',args);

 
Answer from Alana Storm on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › this
this - JavaScript | MDN
September 18, 2025 - The this keyword refers to the context where a piece of code, such as a function's body, is supposed to run. Most typically, it is used in object methods, where this refers to the object that the method is attached to, thus allowing the same method to be reused on different objects.
🌐
W3Schools
w3schools.com › js › js_this.asp
JavaScript this in Objects
In JavaScript, the this keyword refers to an object.
Discussions

javascript - How does "this" keyword work within a function? - Stack Overflow
I just came across an interesting situation in JavaScript. I have a class with a method that defines several objects using object-literal notation. Inside those objects, the this pointer is being... More on stackoverflow.com
🌐 stackoverflow.com
Review my understanding of the 'this' keyword
Everybody seems to explain the ‘this’ keyword differently and many of the explanations seem to conflict with each other and all of them only partially and superficially explain it. The only unifying principle that ‘this’ is founded on seems to be the ‘execution context’ which I’m ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
August 8, 2020
Cannot understand "this" keyword
There's an easy way to remember its usage for most cases: "This" refers to whatever's on the left side of the dot. Example: someObject.someMethod In this example, someMethod has "this" inside it. Since someObject is on the left side of the dot (someObject (dot) someMethod) "this" will refer to someObject. An ELI5 way of explaining it, but hopefully it helps More on reddit.com
🌐 r/learnjavascript
57
88
December 18, 2022
Can someone ELI5 the "this" keyword in JavaScript?

I don't think there is an ELI5 possible for the 'this' keyword in JavaScript. It can refer to many different things, although most likely it is referring to the object that called the function in which 'this' appears. Avoid whenever possible.

More on reddit.com
🌐 r/learnprogramming
10
3
March 30, 2017
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-this-keyword
JavaScript this Keyword - GeeksforGeeks
The 'this keyword' in JavaScript refers to the object to which it belongs. Its value is determined by how a function is called, making it a dynamic reference.
Published   July 11, 2025
Top answer
1 of 7
578

Cannibalized from another post of mine, here's more than you ever wanted to know about this.

Before I start, here's the most important thing to keep in mind about Javascript, and to repeat to yourself when it doesn't make sense. Javascript does not have classes (ES6 class is syntactic sugar). If something looks like a class, it's a clever trick. Javascript has objects and functions. (that's not 100% accurate, functions are just objects, but it can sometimes be helpful to think of them as separate things)

The this variable is attached to functions. Whenever you invoke a function, this is given a certain value, depending on how you invoke the function. This is often called the invocation pattern.

There are four ways to invoke functions in javascript. You can invoke the function as a method, as a function, as a constructor, and with apply.

As a Method

A method is a function that's attached to an object

var foo = {};
foo.someMethod = function(){
    alert(this);
}

When invoked as a method, this will be bound to the object the function/method is a part of. In this example, this will be bound to foo.

As A Function

If you have a stand alone function, the this variable will be bound to the "global" object, almost always the window object in the context of a browser.

 var foo = function(){
    alert(this);
 }
 foo();
 

This may be what's tripping you up, but don't feel bad. Many people consider this a bad design decision. Since a callback is invoked as a function and not as a method, that's why you're seeing what appears to be inconsistent behavior.

Many people get around the problem by doing something like, um, this

var foo = {};
foo.someMethod = function (){
    var that=this;
    function bar(){
        alert(that);
    }
}

You define a variable that which points to this. Closure (a topic all its own) keeps that around, so if you call bar as a callback, it still has a reference.

NOTE: In use strict mode if used as function, this is not bound to global. (It is undefined).

As a Constructor

You can also invoke a function as a constructor. Based on the naming convention you're using (TestObject) this also may be what you're doing and is what's tripping you up.

You invoke a function as a Constructor with the new keyword.

function Foo(){
    this.confusing = 'hell yeah';
}
var myObject = new Foo();

When invoked as a constructor, a new Object will be created, and this will be bound to that object. Again, if you have inner functions and they're used as callbacks, you'll be invoking them as functions, and this will be bound to the global object. Use that var that = this trick/pattern.

Some people think the constructor/new keyword was a bone thrown to Java/traditional OOP programmers as a way to create something similar to classes.

With the Apply Method

Finally, every function has a method (yes, functions are objects in Javascript) named "apply". Apply lets you determine what the value of this will be, and also lets you pass in an array of arguments. Here's a useless example.

function foo(a,b){
    alert(a);
    alert(b);
    alert(this);
}
var args = ['ah','be'];
foo.apply('omg',args);

 
2 of 7
36

Function calls

Functions are just a type of Object.

All Function objects have call and apply methods which execute the Function object they're called on.

When called, the first argument to these methods specifies the object which will be referenced by the this keyword during execution of the Function - if it's null or undefined, the global object, window, is used for this.

Thus, calling a Function...

whereAmI = "window";

function foo()
{
    return "this is " + this.whereAmI + " with " + arguments.length + " + arguments";
}

...with parentheses - foo() - is equivalent to foo.call(undefined) or foo.apply(undefined), which is effectively the same as foo.call(window) or foo.apply(window).

>>> foo()
"this is window with 0 arguments"
>>> foo.call()
"this is window with 0 arguments"

Additional arguments to call are passed as the arguments to the function call, whereas a single additional argument to apply can specify the arguments for the function call as an Array-like object.

Thus, foo(1, 2, 3) is equivalent to foo.call(null, 1, 2, 3) or foo.apply(null, [1, 2, 3]).

>>> foo(1, 2, 3)
"this is window with 3 arguments"
>>> foo.apply(null, [1, 2, 3])
"this is window with 3 arguments"

If a function is a property of an object...

var obj =
{
    whereAmI: "obj",
    foo: foo
};

...accessing a reference to the Function via the object and calling it with parentheses - obj.foo() - is equivalent to foo.call(obj) or foo.apply(obj).

However, functions held as properties of objects are not "bound" to those objects. As you can see in the definition of obj above, since Functions are just a type of Object, they can be referenced (and thus can be passed by reference to a Function call or returned by reference from a Function call). When a reference to a Function is passed, no additional information about where it was passed from is carried with it, which is why the following happens:

>>> baz = obj.foo;
>>> baz();
"this is window with 0 arguments"

The call to our Function reference, baz, doesn't provide any context for the call, so it's effectively the same as baz.call(undefined), so this ends up referencing window. If we want baz to know that it belongs to obj, we need to somehow provide that information when baz is called, which is where the first argument to call or apply and closures come into play.

Scope chains

function bind(func, context)
{
    return function()
    {
        func.apply(context, arguments);
    };
}

When a Function is executed, it creates a new scope and has a reference to any enclosing scope. When the anonymous function is created in the above example, it has a reference to the scope it was created in, which is bind's scope. This is known as a "closure."

[global scope (window)] - whereAmI, foo, obj, baz
    |
    [bind scope] - func, context
        |
        [anonymous scope]

When you attempt to access a variable this "scope chain" is walked to find a variable with the given name - if the current scope doesn't contain the variable, you look at the next scope in the chain, and so on until you reach the global scope. When the anonymous function is returned and bind finishes executing, the anonymous function still has a reference to bind's scope, so bind's scope doesn't "go away".

Given all the above you should now be able to understand how scope works in the following example, and why the technique for passing a function around "pre-bound" with a particular value of this it will have when it is called works:

>>> baz = bind(obj.foo, obj);
>>> baz(1, 2);
"this is obj with 2 arguments"
🌐
web.dev
web.dev › learn › javascript › functions › this
The this keyword | web.dev
The keyword this refers to the value of the object that is bound to the function at the time of its call, meaning that its value is different depending on whether a function is called as a method, as a standalone function, or as a constructor.
Find elsewhere
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Review my understanding of the 'this' keyword - JavaScript - The freeCodeCamp Forum
August 8, 2020 - Everybody seems to explain the ‘this’ keyword differently and many of the explanations seem to conflict with each other and all of them only partially and superficially explain it. The only unifying principle that ‘this’ is founded on seems to be the ‘execution context’ which I’m ...
🌐
Medium
medium.com › codex › understanding-this-in-javascript-the-complete-guide-c4c21fe15ff8
Understanding “this” in JavaScript — The Complete Guide | by Jainishshah | CodeX | Medium
July 28, 2021 - In case a construction function don’t return an object the “new” keyword will create a brand new object and return it. But where does “this” comes into picture ? It turns out when you call a function with “new” keyword .
🌐
Medium
medium.com › @omkarbhavare2406 › understanding-this-keyword-70f846594fd9
Understanding “this” Keyword. The this keyword in JavaScript is… | by Omkar Bhavare | Medium
October 8, 2025 - this.age = 26; const school = "NMV"; const person = { name: 'Omkar', sayName: () => { console.log(this); // { age : 26 } console.log(this.name); // undefined console.log(this.age); // 26 console.log(this.school); // undefined } }; person.sayName(); A constructor function in JavaScript is used to create objects, and when used with the new keyword, this refers to the new object being created.
🌐
JavaScript in Plain English
javascript.plainenglish.io › mastering-the-this-keyword-in-javascript-fd964be1dead
Mastering the ‘this’ Keyword in JavaScript | by Roshan Navale | JavaScript in Plain English
April 29, 2025 - At its core, this is a keyword that refers to the object that is executing the current function. The value of this is not set by how or where the function was defined, but by how it is called.
🌐
DEV Community
dev.to › giftintech › understanding-the-this-keyword-in-javascript-4ipi
Understanding the `this` Keyword in JavaScript - DEV Community
November 5, 2025 - We’ll build a mini visualiser that lets you click buttons to see what this refers to in each context. <div class="container"> <h2>JavaScript "this" Visualiser</h2> <p>Click the buttons below to see what <code>this</code> refers to!</p> <button id="globalBtn">Global Scope</button> <button id="methodBtn">Object Method</button> <button id="regularBtn">Regular Function</button> <button id="arrowBtn">Arrow Function</button> <button id="eventBtn">Event Listener</button> <div id="output" class="output">Click a button to see the result 👇</div> </div>
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › objects: the basics
Object methods, "this"
If you come from another programming language, then you are probably used to the idea of a “bound this”, where methods defined in an object always have this referencing that object. In JavaScript this is “free”, its value is evaluated at call-time and does not depend on where the method was declared, but rather on what object is “before the dot”.
🌐
LoginRadius
loginradius.com › home
Breaking down the 'this' keyword in Javascript
October 6, 2020 - The global object refers to the owner of the 'this' keyword in this case. When it is in a browser window, this global object refers to the window object. ... Since that is true, if you make a strict comparison between the value of this and the window object, we get the boolean value of true. If you run this javascript file inside your computer using a tool like node, this keyword refers to an object of type of object, like so......
🌐
freeCodeCamp
freecodecamp.org › news › the-this-keyword-in-javascript
How to Use the "this" Keyword in JavaScript
April 2, 2025 - Ok, so let's start by defining what the this keyword is. In JavaScript, the this keyword always refers to an object.
🌐
Quora
quora.com › How-do-I-master-using-this-keyword-in-JavaScript
How to master using 'this' keyword in JavaScript - Quora
Answer: The fact of the matter is “this" is becoming less and less important with modern frameworks outside of the Vue options API which is simple where “this" refers to the options object in the Vue component.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Grammar_and_types
Grammar and types - JavaScript | MDN
This is to provide a consistent interface among various JavaScript runtimes. Consequently, you can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable called phoneNumber is declared in a document, you can refer to this variable from an iframe as parent.phoneNumber. You can create a read-only, named constant with the const keyword.
🌐
Dmitri Pavlutin
dmitripavlutin.com › gentle-explanation-of-this-in-javascript
Gentle Explanation of "this" in JavaScript
October 24, 2020 - At the time when new Bar() is executed, JavaScript creates an empty object and makes it the context of the constructor() method. Now you can add properties to object using this keyword: this.property = 'Default Value'.
🌐
Codedamn
codedamn.com › news › javascript
What is ‘this’ keyword in JavaScript? Complete guide
July 18, 2023 - In JavaScript, 'this' is a keyword that refers to the context in which a function is called.
🌐
Reddit
reddit.com › r/learnjavascript › cannot understand "this" keyword
r/learnjavascript on Reddit: Cannot understand "this" keyword
December 18, 2022 -

My head is going to explode because of this. I watched several videos, read articles from MDN, W3schools, and TOP, and I still can't understand.

There's so many values and scenarios around it and I feel like they're explained so vaguely! I struggle to get familiar with it. Can someone drop their own explanation?

Top answer
1 of 5
90
There's an easy way to remember its usage for most cases: "This" refers to whatever's on the left side of the dot. Example: someObject.someMethod In this example, someMethod has "this" inside it. Since someObject is on the left side of the dot (someObject (dot) someMethod) "this" will refer to someObject. An ELI5 way of explaining it, but hopefully it helps
2 of 5
68
Most explanations for this are I think overly technical. You don't need to know what an "execution context" is in order to understand this. Here is the way to think of it: It works like a function parameter Instead of being between the parentheses like other parameters, it is the the thing to the left of the dot Except when it is not It is #3 that really throws people, but that is best understood as short list of exceptions, not some fundamental property of this, and we'll get to it in a minute. Let's start with #1 and #2 which encompass 90% of the instances you will encounter this. A function parameter to the left of the dot So normally you put function parameters between the parentheses when you call it. function greet(user) { console.log('Hello,', user.name); } const sue = { name: 'Sue' }; greet(sue); // Hello, Sue We can easily rewrite this function to instead use this: function greetThis() { console.log('Hello,', this.name); } const bob = { name: 'Bob', greet: greetThis }; bob.greet(); // Hello, Bob Simple enough, right? You can think of it as a way to identify the object that "owns" the function. That is the "context" part of "execution context". The "execution" part means "when the function is called or "executed". For example, we could define the function right inside the object: const ann = { name: 'Ann', greet() { console.log('Hello,', this.name); } }; ann.greet(); // Hello, Ann But remember, this is a function parameter. It only has meaning when the function is called. If we move Ann's greet, this won't point to Ann anymore. const charles = { name: 'Charles', greet: ann.greet }; charles.greet(); // Hello, Charles Okay. That is the basic behavior. If you understand that, you understand most of it. Let's talk about the weirdness and exceptions. When there is nothing to the left of the dot Okay, so what happens when there is nothing to the left of the dot? Well, the unfortunate answer is it depends. If you are in "strict mode" (which includes ES modules), then this will very sensibly be undefined. 'strict mode'; greetThis(); // TypeError: Cannot read properties of undefined (reading 'name') This works exactly like regular function parameters when you don't pass them in between the parentheses. Unfortunately, if you are not in strict mode this very weirdly defaults to the "global context", i.e. window in the browser and global in Node. We could talk more about this, but all you really need to understand is that it is silly and weird and if it happens, you screwed something up. This most often comes up when passing an object method as a callback. 'strict mode'; setTimeout(ann.greet, 1000); // TypeError: Cannot read properties of undefined (reading 'name') Here we passed our function to setTimeout, but we did not pass Ann. So when the greet method gets called by setTimeout, there will not be anything to the left of the dot. This is often solved by wrapping everything in another function. setTimeout(function() { ann.greet(); }, 1000); // Hello, Ann Now when greet is called, Ann will be to the left of the dot. In a class constructor class User { constructor(name) { this.name = name; } } const theresa = new User('Theresa'); Okay. So what gives here? We used this, but there was no dot when we called User. So the new keyword causes functions to be called in "constructor mode". You can think of this as secretly re-assigning this to a new object and then returning it at the end. class User { constructor(name) { // this = {} this.name = name; // return this } } Call, apply, and bind Functions in JavaScript have three methods which let you mess with how they are called. They all take a value for this as their first argument, which means they are often associated with this, but people often forget that the later arguments are all values for the other function parameters. function greetCustom(greeting) { console.log(greeting, this.name); } // Normal parameters ann.greet = greetCustom; ann.greet('Hey'); // Hey Ann // Call greetCustom.call(bob, 'Ciao'); // Ciao Bob // Apply greetCustom.apply(sue, ['Yo']); // Yo Sue // Bind const greetCharles = greetCustom.bind(charles, 'Salutations'); greetCharles(); // Salutations Charles You can look up these three methods if you want, but for the most part you won't need them in modern JavaScript. Nonetheless, it is worth mentioning that if you use them, they change the normal rules for function parameters (including this). Arrow functions Finally, it is worth mentioning that arrows functions do not have a this. If you use this inside an arrow function, it will not take any value related to how you called the arrow function. Instead it will behave like any variable which is not defined in a nested scope. Variable lookup will fallback to the wrapping scope and use the one defined there. 'use strict'; function greetNested() { // this is defined in this function scope // but has no value when called function greet() { console.log('Hello,', this.name); } greet(); } ann.greet = greetNested; ann.greet(); // TypeError: Cannot read properties of undefined (reading 'name') function greetNestedArrow() { // this does not exist in this arrow scope // and so falls back to the wrapping function const greet = () => console.log('Hello,', this.name); greet(); } ann.greet = greetNestedArrow; ann.greet(); // Hello, Ann And that's it. Just remember: this is the thing to the left of the dot when a function is called... except when its not
🌐
AlmaBetter
almabetter.com › bytes › tutorials › javascript › this-keyword-in-javascript
This Keyword in JavaScript
March 15, 2025 - In JavaScript, the 'this' keyword refers to the object that is currently executing or calling the function.