JavaScript Medium technical 0 views 2 min read

What is the purpose of the this keyword in JavaScript?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of JavaScript conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

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

  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

  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

  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

   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

        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

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

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?