How the keyword this works in Javascript? Explain it with basic example

What is this in Javascript

In Javascript the keyword this normally refers to the object that owns the method, but it depends on how a function is called.

In global context

In the global context this refers to the global object both in strict mode and non strict mode. Following are few code snippet in global context for the keyword this 1. this.document is equal to document so console.log(this.document === document); // true
2. As window object is a global object so it will be equal to this console.log(this === window); // true

Comments