What is “this” value in JavaScript?

In JavaScript a function’s this keyword value is –

  1. determined by how a function is called. 
  2. never static and has a different value depending on how the function was invoked.

Below are the this values in different execution context- 

  • Global Context– In the global execution context (outside of any function), this refers to the global object, whether in strict mode or not.
  • Function Context – If code is not in strict mode, and because the value of this is not set by the call, this will default to the global object- In strict mode, however, the value of this remains at whatever it was set to when entering the execution context, so, in the following case, this will default to undefined 
    So, in strict mode, if this was not defined by the execution context, it remains undefined.
  • Custom value- To pass this value from one context to another we can use call or apply functionsWhere a function uses the this keyword in its body, its value can be bound to a particular object in the call using the call or apply methods which all functions inherit from Function.prototype