Table of Contents
In JavaScript, the this keyword refers to an object.
Which object depends on how this is being invoked (used or called).
The this keyword refers to different objects depending on how it is used:
this refers to the objectthis refers to the global objectthis refers to the global objectthis is undefinedthis refers to the element that received the eventcall(), apply(), and bind() can refer this to any objectWhen used in an object method, this refers to the object:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript this</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>this in an Object Method</h4>
<p id="demo"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe",
id: 5566,
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
document.getElementById("demo").innerHTML = person.fullName();
</script>
</body>
</html>

Result View Example
In a regular function, this refers to the global object (window in a browser):
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript this</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>this in a Function (Default)</h4>
<p id="demo"></p>
<script>
function myFunction() {
return this;
}
document.getElementById("demo").innerHTML = myFunction();
</script>
</body>
</html>

Result View Example
When used alone, this refers to the global object (window in a browser):
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript this</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>this Alone</h4>
<p id="demo"></p>
<script>
let x = this;
document.getElementById("demo").innerHTML = "this alone: " + x;
</script>
</body>
</html>

Result View Example
In HTML event handlers, this refers to the HTML element that received the event:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript this</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>this in Event Handlers</h4>
<button onclick="this.style.display='none'">Click to remove me!</button>
<br><br>
<button id="myBtn">Click to hide (using JS)</button>
<script>
document.getElementById("myBtn").addEventListener("click", function() {
this.style.display = 'none';
});
</script>
</body>
</html>

Result View Example
Arrow functions do NOT have their own this. Instead, they inherit this from the surrounding scope.
This makes arrow functions useful in callbacks and methods where you want to preserve the context:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript this</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>this in Arrow Functions</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
// Regular function - has its own this
const obj1 = {
name: "Object 1",
greet: function() {
return "Hello from " + this.name;
}
};
// Arrow function - inherits this from surrounding scope
const obj2 = {
name: "Object 2",
greet: () => {
return "Hello from " + this.name; // this is NOT obj2
}
};
document.getElementById("demo1").innerHTML = obj1.greet();
document.getElementById("demo2").innerHTML = obj2.greet() + " (arrow function - this is window)";
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.