JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

The this Keyword

javascript
mid
this

How does the 'this' keyword work in JavaScript?

this
scope
binding
call
apply
bind
arrow-functions
Quick Answer

The value of 'this' depends on how a function is called: in a method, it refers to the object; in a regular function, it's the global object (or undefined in strict mode); in arrow functions, it's lexically inherited from the enclosing scope.

Detailed Explanation

'this' binding rules (in order of precedence):

  1. new binding: When called with 'new', 'this' is the newly created object

  2. Explicit binding: Using call(), apply(), or bind() explicitly sets 'this'

  3. Implicit binding: When called as object method, 'this' is the object

  4. Default binding: In non-strict mode, 'this' is global object; in strict mode, undefined

Arrow functions:

  • Don't have their own 'this'
  • Inherit 'this' from enclosing lexical scope
  • Cannot be changed with call/apply/bind

Common pitfalls:

  • Losing 'this' in callbacks
  • Event handlers in classes
  • Nested functions

Code Examples

Different 'this' contextsJavaScript
// 1. Method call - 'this' is the object
const obj = {
  name: 'Alice',
  greet() {
    console.log(this.name);
  }
};
obj.greet(); // 'Alice'

// 2. Function call - 'this' is global/undefined
const greet = obj.greet;
greet(); // undefined (strict) or window.name

// 3. Constructor call - 'this' is new instance
function Person(name) {
  this.name = name;
}
const bob = new Person('Bob');
console.log(bob.name); // 'Bob'

// 4. Arrow function - 'this' from lexical scope
const obj2 = {
  name: 'Charlie',
  greet: () => {
    console.log(this.name); // 'this' from outer scope
  }
};
obj2.greet(); // undefined (not obj2)

Real-World Applications

Use Cases

Event Handlers

Ensuring correct this binding in DOM event callbacks and React class components to access instance properties

Fluent APIs / Method Chaining

Using return this in methods to enable chaining like builder.setName('x').setAge(25).build()

Framework Integration

Understanding this in Vue options API, Backbone models, and other OOP frameworks that rely on this-based patterns

Mini Projects

Fluent Builder

beginner

Build a query or config builder that uses return this for method chaining with a final build() method

Binding Debugger

intermediate

Build a tool that demonstrates how this changes across different call styles (method, function, new, arrow, bind)

Industry Examples

jQuery

Fluent API using return this in every method enabling chains like $('div').addClass('x').show().fadeIn()

Vue.js

Options API uses this to access reactive data, methods, and computed properties within component methods

Resources

MDN - this

docs

JavaScript.info - Object methods, this

article

You Don't Know JS - Objects & Classes

book

Related Questions

What is a closure in JavaScript and why are they useful?

mid
closures

Explain JavaScript's prototype chain and how inheritance works.

senior
prototypes
Previous
Explain the JavaScript Event Loop and how it handles asynchronous operations.
Next
What is the difference between shallow copy and deep copy in JavaScript, and how do you create each?
PrevNext