Dec 21, 2016Last modified July 7, 2025

Understanding this in javascript

'this' is a keyword in javascript.

If you have been programming in java before, yo would know that 'this' always refers to the object which is calling the method.

But in javascript, its different.

Rather than 'who is calling', the value of 'this' changes based on the context from which the method is called.

Global Context

By default, 'this' refers to the global object. So if you do this === window in your console tab in browser, you will get true.

Function Context

When you refer this is a function, it still equals to the window object. Unless and until you are using strict mode, which is when it is 'undefined'.

Method Context

When a function is called as a method of an object, this refers to the object.

Event Handlers

In DOM event handlers, this refers to the element that received the event.

document.querySelector('button').addEventListener('click', function() {
console.log(this); // The button element
});
document.querySelector('button').addEventListener('click', function() {
console.log(this); // The button element
});

Arrow functions

Arrow functions don't have their own this - they inherit it from the surrounding lexical context.

const obj = {
name: 'My Object',
regularMethod: function() {
console.log(this); // obj
const arrowInside = () => {
console.log(this); // obj (inherited from regularMethod)
};
arrowInside();
},
arrowMethod: () => {
console.log(this); // window (inherited from global scope)
}
};

obj.regularMethod();
obj.arrowMethod();
const obj = {
name: 'My Object',
regularMethod: function() {
console.log(this); // obj
const arrowInside = () => {
console.log(this); // obj (inherited from regularMethod)
};
arrowInside();
},
arrowMethod: () => {
console.log(this); // window (inherited from global scope)
}
};

obj.regularMethod();
obj.arrowMethod();

Explicit Binding

You can explicitly set this using:

  • call(): Set this and call the function immediately with individual arguments
  • apply(): Like call(), but takes arguments as an array. this binding is limited for the function call.
  • bind(): Returns a new function with this permanently bound. Unlike call and apply, bind doesnt invoke the function immediately. Additionally the this binding is permanent. Once a function is created with bind(), you cannot change its this context using call() or apply() - the binding is permanent for that new function.
function greet() {
console.log(`Hello, ${this.name}`);
}

const person = { name: 'Alice' };

// call() and apply() immediately invoke the function
greet.call(person); // Hello, Alice
greet.apply(person); // Hello, Alice

// bind() creates a new function with bound this
const boundGreet = greet.bind(person);
boundGreet(); // Hello, Alice
function greet() {
console.log(`Hello, ${this.name}`);
}

const person = { name: 'Alice' };

// call() and apply() immediately invoke the function
greet.call(person); // Hello, Alice
greet.apply(person); // Hello, Alice

// bind() creates a new function with bound this
const boundGreet = greet.bind(person);
boundGreet(); // Hello, Alice