JavaScriptEasy
Explain how this works in JavaScript
By FrontendPro Editorial Team Updated 8/14/2026
#JavaScript
Answer
There's no simple explanation for this; it is one of the most confusing concepts in JavaScript because its behavior differs from many other programming languages. The one-liner explanation of the this keyword is that it is a dynamic reference to the context in which a function is executed.
A longer explanation is that this follows these rules:
- If the
newkeyword is used when calling the function, meaning the function was used as a function constructor, thethisinside the function is the newly-created object instance. - If
thisis used in aclassconstructor, thethisinside theconstructoris the newly-created object instance. - If
apply(),call(), orbind()is used to call/create a function,thisinside the function is the object that is passed in as the argument. - If a function is called as a method (e.g.
obj.method()) -thisis the object that the function is a property of. - If a function is invoked as a free function invocation, meaning it was invoked without any of the conditions present above,
thisis the global object. In the browser, the global object is thewindowobject. If in strict mode ('use strict';),thiswill beundefinedinstead of the global object. - If multiple of the above rules apply, the rule that is higher wins and will set the
thisvalue. - If the function is an ES2015 arrow function, it ignores all the rules above and receives the
thisvalue of its surrounding scope at the time it is created.
For an in-depth explanation, do check out Arnav Aggrawal's article on Medium.
