JavaScript Object this

Back to JS page

Table of Contents

this in Objects

In JavaScript, the this keyword refers to an object.

Which object depends on how this is being invoked (used or called).

The this keyword refers to different objects depending on how it is used:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object this</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>this in Objects</h4>
<p id="demo"></p>

<script>
// Create an object
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
};

// Using 'this' would refer to the global object here
// But when used in a method, 'this' refers to the object

document.getElementById("demo").innerHTML =
  "person.firstName: " + person.firstName + "<br>" +
  "person.lastName: " + person.lastName;
</script>

</body>
</html>

Example 1

Result View Example

this in an Object Method

When used in an object method, this refers to the object itself:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object this</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>this in an Object Method</h4>
<p id="demo"></p>

<script>
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30,
  fullName: function() {
    return this.firstName + " " + this.lastName;
  },
  birthYear: function() {
    const year = new Date().getFullYear();
    return year - this.age;
  }
};

document.getElementById("demo").innerHTML =
  "Full name: " + person.fullName() + "<br>" +
  "Born in: " + person.birthYear();
</script>

</body>
</html>

Example 2

Result View Example

Why Use this?

The this keyword allows you to reuse methods across different objects without rewriting the code:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object this</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Why Use this?</h4>
<p id="demo"></p>

<script>
// A method that uses 'this' can work with any object
function introduce() {
  return "Hi, I'm " + this.firstName + " " + this.lastName;
}

const person1 = {
  firstName: "John",
  lastName: "Doe",
  introduce: introduce
};

const person2 = {
  firstName: "Jane",
  lastName: "Smith",
  introduce: introduce
};

document.getElementById("demo").innerHTML =
  person1.introduce() + "<br>" +
  person2.introduce();
</script>

</body>
</html>

Example 3

Result View Example

this Alone

When used alone, this refers to the global object (window in a browser):

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object this</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>this Alone</h4>
<p id="demo"></p>

<script>
// In a browser, 'this' alone refers to the window object
let text = "this (global): " + this + "<br>";
text += "this === window: " + (this === window) + "<br>";
text += "this.document === document: " + (this.document === document);

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 4

Result View Example

this in a Function

In a regular function, this refers to the global object (window in a browser):

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object this</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>this in a Function</h4>
<p id="demo"></p>

<script>
function myFunction() {
  return this;
}

document.getElementById("demo").innerHTML =
  "Function returns: " + myFunction() + "<br>" +
  "It's the window object: " + (myFunction() === window);
</script>

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference