One of the most enigmatic and confusing concepts to beginners in JavaScript (JS) is the keyword ‘this’. What makes it so confusing is the fact that it behaves differently depending on the context in which it is used. The good news is that once you understand its nuances, you will find that ‘this’ is actually not that complicated. Let’s jump right into it!

What is ‘this’?

‘This’ is a keyword in the JavaScript language that refers to an object. This object can be one of two things:

  1. The ‘window’ object
  2. An instance of an object

1. The window object

The window object represents the browser itself in the Document Object Model (DOM) . It holds the highest position in the DOM hierarchy, as it is a parent of the document object and all its children . ‘This’ can refer to the window object in two cases:

  • When used in a global context
  • When used in a baseless function

#javascript #react #function #programming #web-development

Demystifying JavaScript’s ‘This’
1.05 GEEK