JavaScript this Keyword
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.
Example
const person = {
<<<<<<< HEAD
name: "Coding75",
=======
name: "JavaScript Hub",
>>>>>>> 6c9caef (content Overview Complete)
greet() {
return `Welcome To, ${this.name}`;
}
};
console.log(person.greet());Output:
<<<<<<< HEAD
Welcome To, Coding75
=======
Welcome To, JavaScript Hub
>>>>>>> 6c9caef (content Overview Complete)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.
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.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
}function greet() {
console.log('Hello, my name is ' + this.name);
}
const person = {
name: 'Amit',
sayHello: greet
};
const anotherPerson = {
name: 'Jatin'
};
person.sayHello();
greet.call(anotherPerson);Output:
Hello, my name is undefined
Hello, my name is Amit
Hello, my name is JatinUsing this Alone (Global Context)
When used alone in JavaScript, the behavior of the this keyword depends on whether the code is running in strict mode or not.
console.log(this);Output:
{}Implicit Binding
When we call a function as a method of the object, the this keyword refers to the calling object.
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 oldExplicit Binding
When we explicitly bind the this keyword using the call(), bind(), or apply() method, the this reference changes to the object passed to these methods.
function ageVerify() {
console.log(this.age >= 18 ? "Yes you can drive" : "No you cannot drive");
}
const per1 = { age: 20 };
const per2 = { age: 16 };
ageVerify.call(per1);
ageVerify.call(per2);Output:
Yes you can drive
No you cannot driveDefault Binding
When the this keyword is used in the global scope, it is set to the window object.
const age = 22;
function verifyAge() {
return this.age;
}
console.log(verifyAge());Output:
undefinedArrow Function Binding
When this is used in an arrow function, it has lexical scope, so it inherits this from its surrounding context.
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 oldSummary of this Keyword Behavior
- When used within a method of an object,
thispoints to that object. - When used by itself,
thispoints to the global object. - Within a function,
thistypically points to the global object. - In a function under strict mode,
thisbecomesundefined. - During an event,
thispoints to the element that triggered the event. - Methods such as
call(),apply(), andbind()can reassignthisto any desired object.
The Precedence Order of this Keyword
- JavaScript
bind()Method - JavaScript
call()andapply()Method - JavaScript Object Method
- JavaScript Global Scope
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, it points to the global object (like
window). - In strict mode, it is
undefined. - It can be reassigned with
call(),apply(), orbind().
This .mdx file properly formats the content with JavaScript code blocks and Markdown syntax for better readability and structuring.