14 Tweets 1 reads Apr 12, 2023
JavaScript is a powerful language.
Especially when you use the hidden 'this' keyword.
Discover the power of the 'this' keyword and how to leverage it in your JavaScript projects:
"this" keyword is used to refer to the object that function belongs to or the object that the function is called on. The value of "this" is dynamic and changes depending on the context in which it is used.
There are four different contexts in which `this` is used in JavaScript:
1. Global context
In the global context, `this` refers to the global object, which is `window` in the browser and `global` in Node.js.
2. Function context
In the function context, `this` refers to the object that the function is called on. If the function is not called on an object, `this` refers to the global object.
In this example, `this` refers to the `person` object when the `sayHello` method is called on the `person` object. However, when the `sayHello` method is assigned to the `sayHello` variable and called, `this` no longer refers to the `person` object,
1/2
But instead refers to the global object, which results in `undefined` being logged to the console.
To fix this, we can use the `bind()` method to bind the `person` object to the `sayHello` method.
2/2
3. Method context
In the method context, `this` refers to the object that the method belongs to.
In this example, `this` refers to the `address` object when the `getFullAddress` method is called on the `address` object.
4. Constructor context
In the constructor context, `this` refers to the instance of the object that is being created.
In the above example, `this` refers to the instance of the `Person` object that is being created when the new keyword is used to create a `new` instance of the `Person` object.
Conclusion 💡
`this` keyword is an essential tool in JavaScript that allows functions to access the object they belong to, and to access the object's properties and methods. However, the value of `this` can be tricky to understand, as it changes depending
Ready to take the first step towards your dream job or internship in the tech industry? Join our pool and fill out the form now!
🔗 pooools.com
You can get my free JavaScript e-book that has 100+ free resources.
🔗 haiderkh1.gumroad.com
That's a wrap!
If you enjoyed this thread:
1. Follow me @slow_developer for more of these
2. RT the tweet below to share this thread with your audience

Loading suggestions...