Open In App

JavaScript this Keyword

In JavaScript, this keyword refers to the current context or scope within which code is executing. Its value is determined by how a function is called, and it can dynamically change depending on the invocation context.

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



JavaScript this Keyword Examples:

1. Using this in a Method

In the context of an object method in JavaScript, the `this` keyword refers to the object itself, allowing access to its properties and methods within the method’s scope. It facilitates interaction with the object’s data and behavior, providing a way to access and manipulate its state.

Example: Below is an example of this in Method.






const person = {
  name: 'John',
  age: 30,
  greet() {
    console.log('Hello, my name is ' + this.name + ' and I am ' + this.age + ' years old.');
  }
};
 
person.greet(); // Output: Hello, my name is John and I am 30 years old.

Output
Hello, my name is John and I am 30 years old.

Explanation:

2. Using this in a Function

In a JavaScript function, the behavior of the this keyword varies depending on how the function is invoked.

Syntax:

function exampleFunction() {
console.log(this); // Refers to the current execution context
}

Example: Below is an example of this in a function.




function greet() {
  console.log('Hello, my name is ' + this.name);
}
 
const person = {
  name: 'John',
  sayHello: greet
};
 
const anotherPerson = {
  name: 'Alice'
};
 
greet(); // Output: Hello, my name is undefined
person.sayHello(); // Output: Hello, my name is John
greet.call(anotherPerson); // Output: Hello, my name is Alice

Output
Hello, my name is undefined
Hello, my name is John
Hello, my name is Alice

Explanation:

3. Using this alone

When used alone in JavaScript, outside of any specific context, the behavior of the this keyword depends on whether the code is running in strict mode or not.

Example:




console.log(this);

Output
{}

Explanation:

4. Implicit Binding

When we call a function as a method of the object this keyword refers to the calling object

Example: In this example, we will see the implicit binding of this keyword.




const person = {
    name: "ram",
    age: 22,
    greet: function(){
        return `Hello ${this.name}, you are ${this.age} years old`
    }
}
console.log(person.greet());

Output
Hello ram, you are 22 years old

Output: Here this keyword is referring to the person object so it can access name and age values.

5. Explicit Binding

When we explicitly bind this keyword using the call(), bind(), or apply() method then this keyword default reference is changed to the object called using the above-specified methods.

Example: In this example, we will see the explicit binding of this keyword.




function ageVerify(){
    if(this.age> 18){
        console.log("Yes you can drive");
    } else {
        console.log("No you cannot drive");
    }
}
 
const per1 = {age: 21};
const per2 = {age: 16};
ageVerify.call(per1);
ageVerify.call(per2);

Output
Yes you can drive
No you cannot drive

6. Default Binding

When this keyword is used in global scope this is set to window object.

Example: This example shows the Default binding.




const age = 22;
function verifyAge (){
    return this.age;
}
 
console.log(verifyAge());

Output
undefined

7. Arrow Function Binding

When this is used in the arrow function then this has lexical scope so without the function keyword this is unable to refer to the object in the outer scope. 

Example: This example shows the arrow function binding.




const person = {
    name: "ram",
    age: 22,
    greet : () =>{
        return `Hello , you are ${this.age} years old`
    }
}
console.log(person.greet());

Output
Hello , you are undefined years old

The precedence order of this keyword is:

Supported Browser:

The browser supported are listed below:

We have a complete list of JavaScript reserved words, to learn about them please refer to JavaScript | Reserved Words article.


Article Tags :