This lesson is a quick clarifier of this, and how its value changes depending on the context from which it is called.
JavaScript This Keyword in Different Contexts
The behavior of the JavaScript this keyword varies depending on the context.
Learn by Doing
Experiment with this in the browser console. Try calling it from different places and see what you get.
To learn more about this, check out the following lessons from the JavaScript 101 course where this is covered in more detail in terms of the different contexts:
- Referencing JavaScript Object Properties
- Methods vs Functions
- JavaScript Built-in Global Objects
- Object Instantiation
- Arrow Functions
- JavaScript Events
Summary: Understand the Different Behaviors of the JavaScript This Keyword
- The behavior of the JavaScript
thiskeyword varies depending on the context - The
thiskeyword can be used in different contexts:- Arrow functions
- Methods
- The main script
- Event handlers
- For this course, you'll be fine just using
thiswithin class declarations