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
The this keyword refers to different objects depending on how it is used: ... You cannot change the value of this. When used in an object method, this refers to the object.
Discussions

javascript - How does "this" keyword work within a function? - Stack Overflow
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. Finally, every function has a method (yes, functions are objects in Javascript) named "apply". More on stackoverflow.com
🌐 stackoverflow.com
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
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
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 29, 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.
🌐
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.
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › the-javascript-this-keyword-explained-with-examples
The JavaScript this Keyword Explained with Examples
June 5, 2024 - In JavaScript, a method gets the value of this when it looks at the function that comes before the dot. It's not news that the function constructor was the default initializer for user-defined objects before the introduction of the ECMAScript 2015 update. The new keyword creates an instance of a constructor function:
🌐
freeCodeCamp
freecodecamp.org › news › the-this-keyword-in-javascript
How to Use the "this" Keyword in JavaScript
April 2, 2025 - An important comment is that this is not a variable – it's a keyword, so its value can't be changed or reassigned. If we call this by itself, meaning not within a function, object, or whatever, it will refer to the global window object. If you print it like console.log('this alone', this); you'll get this in your console: [object Window].
🌐
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
🌐
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 .
🌐
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 ...
🌐
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.
🌐
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.
🌐
Dmitri Pavlutin
dmitripavlutin.com › gentle-explanation-of-this-in-javascript
Gentle Explanation of "this" in JavaScript
October 24, 2020 - The same scenario happens when using class syntax (available in ES2015), only the initialization happens in the constructor method: ... 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'.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › demystifying the javascript this keyword
Demystifying JavaScript this Keyword with Practical Examples
August 17, 2025 - You get undefined instead of "Honda" ... object, JavaScript sets this to the global object in non-strict mode and undefined in the strict mode. To fix this issue, you can use the bind() method of the Function.prototype object. The bind() method creates a new function whose the this keyword is set to ...
🌐
ui.dev
ui.dev › this-keyword-call-apply-bind-javascript
Understanding the "this" keyword, call, apply, and bind in JavaScript
In this post you'll learn 5 rules for understanding JavaScript's 'this' keyword as well as JavaScript's bind, call, and apply array methods.
🌐
AlmaBetter
almabetter.com › bytes › tutorials › javascript › this-keyword-in-javascript
This Keyword in JavaScript
In JavaScript, the 'this' keyword refers to the object that is currently executing or calling the function.
🌐
W3Schools
w3schools.com › js › js_reserved.asp
JavaScript Reserved Words
JS Keywords Reference JS Keywords Reserved JS Operator Reference JS Operator Precedence JS Versions · JS Versions JS 2026 JS 2025 JS 2024 JS 2023 JS 2022 JS 2021 JS 2020 JS 2019 JS 2018 JS 2017 JS 2016 JS 2015 (ES6) JS 2009 (ES5) JS 1999 (ES3) JS IE / Edge JS History ... Function Definitions Function this Function Invocation Function IIFE Function Call Function Apply Function Bind Function Closures Function Reference JS Objects