InterviewHubPRO
Section Overview
Q456

Question 456 of 521

Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

88%
JavaScript Internals (521 Qs)•Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

What is the purpose of the this keyword in JavaScript?

The this keyword in JavaScript refers to the object that is executing the current function. Its value is determined by how a function is called, not where it is defined. this is essential for writing object-oriented and event-driven code, as it allows methods to interact with the data of the object they belong to.

Example 1: this in a Global Context

javascript
console.log(this);
  • In a global context, this refers to the global object (e.g., window in a browser).

Example 2: this in a Function

javascript
function displayThis() {
console.log(this);
}

displayThis();
  • In a regular function, this refers to the global object(window in browser and global in nodejs) for non-strict mode. In strict mode, it's value is undefined.

Example 3: this in a Method

javascript
const person = {
name: "John",
greet: function () {
  console.log("Hello, " + this.name);
},
};

person.greet();
  • In a method, this refers to the object that owns the method (person in the case).

Example 4: this in an Event Handler

javascript
document.getElementById("myButton").addEventListener("click", function () {
        console.log(this);
});
  • In an event handler, this refers to the element that triggered the event (the button in this case).

Example 5: this with Arrow Functions

javascript
    const obj = {
      age: 42,
      regular: function() { console.log(this.age); },
      arrow: () => { console.log(this.age); }
    };
    obj.regular(); // 42 (this refers to obj)
    obj.arrow();   // undefined (this refers to the outer scope, not obj)
  • Arrow functions do not have their own this binding; they inherit it from their surrounding (lexical) context.

Example 6: this in Constructor Functions / Classes

javascript
function Person(name) {
  this.name = name;
}

const p1 = new Person('Alex');
console.log(p1.name); // Alex
  • When used with new, this refers to the newly created object.