Last Updated : 11 Jul, 2025
The 'this keyword' in JavaScript refers to the object to which it belongs. Its value is determined by how a function is called, making it a dynamic reference. The 'this' keyword is a powerful and fundamental concept used to access properties and methods of an object, allowing for more flexible and reusable code.
JavaScript
const person = {
name: "GeeksforGeeks",
greet() {
return `Welcome To, ${this.name}`;
}
};
console.log(person.greet());
Welcome To, GeeksforGeeks
There are more ways to use this keyword in JavaScript:
Using this in a MethodIn 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 behaviour, providing a way to access and manipulate its state.
JavaScript
const person = {
name: 'John',
age: 30,
greet() {
console.log('Hello, my name is ' +
this.name + ' and I am '
+ this.age +
' years old.');
}
};
person.greet();
Hello, my name is John and I am 30 years old.Using this in a Function
In a JavaScript function, the behavior of the this
keyword varies depending on how the function is invoked.
Syntax:
JavaScriptfunction exampleFunction() {
console.log(this); // Refers to the current execution context
}
function greet() {
console.log('Hello, my name is ' + this.name);
}
const person = {
name: 'Amit',
sayHello: greet
};
const anotherPerson = {
name: 'Jatin'
};
//Driver Code Starts
greet();
person.sayHello();
greet.call(anotherPerson);
//Driver Code Ends
Hello, my name is undefined Hello, my name is Amit Hello, my name is JatinUsing this alone(Global Context)
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.
Implicit Binding
When we call a function as a method of the object this keyword refers to the calling object
JavaScript
const person = {
name: "Ram",
age: 22,
greet: function () {
return `Hello ${this.name}, you are ${this.age} years old`
}
}
console.log(person.greet());
Hello Ram, you are 22 years old
Here this keyword is referring to the person object so it can access name and age values.
Explicit BindingWhen 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.
JavaScript
//Driver Code Starts
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 };
//Driver Code Ends
ageVerify.call(per1);
ageVerify.call(per2);
Yes you can drive No you cannot driveDefault Binding
When this keyword is used in global scope this is set to window object.
JavaScript
const age = 22;
function verifyAge (){
return this.age;
}
console.log(verifyAge());
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.
JavaScript
const person = {
name: "ram",
age: 22,
greet : () =>{
return `Hello , you are ${this.age} years old`
}
}
console.log(person.greet());
Hello , you are undefined years oldReturns after using this keyword
this
points to that object.this
points to the global object.this
typically points to the global object.this
becomes undefined.this
points to the element that triggered the event.call()
, apply()
, and bind()
can reassign this
to any desired object.The this keyword in JavaScript dynamically refers to the object executing a function or method. Its context varies: in methods, it points to the object itself; in the global scope, to the global object (like window); in strict mode, to undefined; and can be reassigned with call(), apply(), or bind().
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4