🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #51

Object this

this in Objects

The this keyword refers to an object.

In JavaScript, this is used to access the object that is calling a method.

this in an Object Method

When used inside an object method, this refers to the object.

Example

const person = {

  firstName: "John",

  lastName: "Doe",

  age: 50,

  fullName: function() {

    return this.firstName + " " + this.lastName;

  }

};

In the example above:

  • this.firstName refers to the firstName property of the person object
  • this.lastName refers to the lastName property of the person object

Why Use this ?

The this keyword makes it possible to use the same method with different objects.

Example

const person1 = {

  name: "John",

  hello: function() {

    return "Hello " + this.name;

  }

};

const person2 = {

  name: "Anna",

  hello: function() {

    return "Hello " + this.name;

  }

};

document.getElementById("demo").innerHTML = person1.hello();

this Alone

When used alone, this refers to the global object.

In a browser, the global object is the window object.

Example

let x = this;

document.getElementById("demo").innerHTML = x;

Note: In strict mode, this is undefined when used alone.


this in a Function

In a regular function, this also refers to the global object.

Example

function myFunction() {

  return this;

}

document.getElementById("demo").innerHTML = myFunction();

Summary

  • In an object method, this refers to the object
  • this lets methods access object properties
  • Used alone, this refers to the global object

Note: See Also: What are JavaScript Objects? What are Object Properties? What are Object Methods? How to Display JavaScript Objects What is an Object Constructor?


Note: Advanced Chapters: JavaScript Object Definitions JavaScript Object Advanced this JavaScript Object Iterations JavaScript Object Getters & Setters JavaScript Object Management JavaScript Object Protection JavaScript Object Prototypes JavaScript Object Reference

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Object this – FAQs

Quick answers about learning Object this in JavaScript.

This free note from CodingNow 2.0 explains Object this in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including Object this, is 100% free with no signup required.
With focused practice, most students grasp Object this in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now