Introduction

The keyword this is one of the useful features that were introduced in JavaScript. It refers to the object it belongs to and it has different values depending on where it is used. As by the word this itself, we can understand that it’s referring to something.

In this article, we will learn about the keyword this in JavaScript with some practical examples. Let’s get right into it.

Values of ‘this’

As I said, the keyword this has different values depending on where it is used. It works differently in JavaScript from other programming languages.

Here is a list of the values that the keyword this can take:

  • In a method, this refers to the owner object.
  • In a function, this refers to the global object.
  • In a function, in strict mode, this is undefined.
  • Alone, this refers to the global object.
  • In an event, this refers to the element that received the event.
  • Methods like call(), and apply() can refer this to any object.

#javascript #coding #web-development

The Keyword ‘this’ in JavaScript Explained With Examples
2.10 GEEK