JavaScript this Keyword

Example

var person = {
  firstName: "Bill",
  lastName : "Gates",
  id       : 678,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};

Try It Yourself

What is 'this'?

JavaScript this The keyword refers to the object it belongs to.

it has different values depending on where it is used:

  • In a method,this refers to the owner object.
  • in the case of a lone function,this refers to the global object.
  • In a function,this refers to the global object.
  • In a function, in strict mode,this is undefined.
  • In the event,this refers to the element that receives the event.

like call() and apply() Such methods can refer to 'this' for any object.

The 'this' in the method

In object methods,this refers to the "owner" of this method.

In the example at the top of this page,this refers to the person object.

The person object is the owner of the fullName method.

fullName : function() {
  return this.firstName + " " + this.lastName;
}

Try It Yourself

The lone 'this'

When used alone, the owner is the global object, therefore this refers to the global object.

In the browser window, the global object is [object Window]:

Example

var x = this;

Try It Yourself

In strict mode, if used alone, this refers to the global object [object Window]:

Example

"use strict";
var x = this;

Try It Yourself

The 'this' in functions (default)

In JavaScript functions, the default binding of the function's owner this.

Therefore, in a function,this refers to the global object [object Window].

Example

function myFunction() {
  return this;
}

Try It Yourself

The 'this' in functions (strict mode)

JavaScript strict mode does not allow default binding.

Therefore, when used in a function, in strict mode,this is undefined (undefined));

Example

"use strict";
function myFunction() {
  return this;
}

Try It Yourself

The 'this' in event handlers

In HTML event handlers,this refers to the HTML element that receives this event:

Example

<button onclick="this.style.display='none'">
  Click to delete me!
</button>

Try It Yourself

Object Method Binding

In this example,this is the person object (the person object is the "owner" of this function):

Example

var person = {
  firstName  : "Bill",
  lastName   : "Gates",
  id         : 678,
  myFunction : function() {
    return this;
  }
};

Try It Yourself

Example

var person = {
  firstName: "Bill",
  lastName : "Gates",
  id       : 678,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};

Try It Yourself

In other words,this.firstName means thisof the firstName properties.

Explicit Function Binding

call() and apply() Methods are predefined JavaScript methods.

They can both be used to call an object method with another object as an argument.

You can read more about them at the end of this tutorial. call() and apply() for more information.

In the following example, when person2 is used as an argument to call person1.fullName,this will refer to person2, even though it is a method of person1:

Example

var person1 = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}
var person2 = {
  firstName: "Bill",
  lastName: "Gates",
}
person1.fullName.call(person2);  // It will return "Bill Gates"

Try It Yourself