JavaScript this Keyword

Alex H
2 min readSep 15, 2021

In JavaScript, the this keyword references a specific JavaScript element and depends on how a function that uses this is being called. In this blog I will go over the functional and method contexts of this.

The value of this when defined inside of a function depends on how the function is called. Methods like bind, call, and apply can directly tie the value of this to the function that is being called, however if this is not explicitly set, then it will default to the global context instead. The exception is if a function uses strict mode which will automatically make this refer to it’s explicit value and this will be undefined if it has not been set by the function.

Function where this will be global
Function using strict mode. ‘this’ will be undefined

The value of this when defined in a method refers to the owner of the object. In the example below, this refers to the car object. The car object is the direct owner of the model makeModel function.

method where this refers to the object owner car

These are just a few examples of how the value of this can change on where and how the function it resides in is called.

--

--