How i got to understand the "this" keyword in JavaScript

Hello fellow tech mates, am so happy to be writing this short article on the "this" keyword. this.jpg

For a long time i had issues while learning javascript and mainly, i didn't grasp the concept about the "this" keyword until recently when i came across a tutorial by the thenetninjauk, he gave a good and clear understanding about it. The "this " keyword is a context object and it represents the context in which the entire code is executed. (might still sound confusing right, i'll break it down a little more).

The JavaScript this keyword refers to the object it belongs to. It has different values depending on where it is used: . In a function, this refers to the global object. In a function, in strict mode, this is undefined . w3school

let Person = {
  firstName: "dumto",
  lastName : "narth",
  id       : 1134,
  fullName() {
    return this.firstName + " " + this.lastName;
  }
};
console.log(person.fullName());
//Javascript sets the this keyword to be the object the -
// method was used on

In an object method, this refers to the "owner" of the method, and who is the owner of the fullName method (it's the Person object). In the example above, this refers to the Person object.

Thank you so much if you took out your time to read this, would still be writing more articles so sit back and relax

Dumto

Young dude who loves coding and i'm having a thing for writing about my journey in tech. So just sit back and relax.

Write your comment…

Dumto, Nice Start. I think you have mentioned the catch line very well:

The "this " keyword is a context object and it represents the context in which the entire code is executed.

You can easily extend this post or write future series on this with more examples and it's confusing characters.

would do that certainly and i'll get to do more research on it for better understanding

Reply to this…