Kevin Powell - CommunityKP-C
Kevin Powell - Communityโ€ข15mo agoโ€ข
9 replies
Faker

Understanding the "this" keyword for objects in JavaScript

Hello guys, can someone explain why it is important to use the "this" keyword here, what would happen if we omit it please.

Also, I notice that when we create a class, for e.g. say class Person {. . .}, in this case, I don't remember we would have use the keyword this. I know that the this keyword is use to reference the current object but in the example below, don't we have a single object? So can't we omit the "this" keyword?

const person = {
    name: ['John','Doe'],
    age: 21,
    bio: function() {
        console.log(`Hi, my name is ${this.name[0]} ${this.name[1]}, nice to meet you all. `);
    },
    introduceSelf: function() {
        console.log(`Hello, I'm ${this.name[0]}`);
    },
};
Was this page helpful?