
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Explain JavaScript "this" keyword?
The JavaScript this keyword references the object to which it belongs. It can refer to the global object if alone or inside a function. It refers to the owner object if inside a method and refers to the HTML element that received the event in an event listener.
Example
Following is the code for the JavaScript this Identifier −
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 18px; font-weight: 500; color: red; } </style> </head> <body> <h1>JavaScript this Identifier</h1> <div class="sample"></div> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3> Click on the above button to see which object 'this' refers to in multiple context </h3> <script> let thisRef = this; let sampleEle = document.querySelector(".sample"); function test() { return this; } let testObj = { a: 22, check() { return this; }, }; document.querySelector(".Btn").addEventListener( "click", () => { sampleEle.innerHTML = "This inside normal function = " + test() + "<br>"; sampleEle.innerHTML += "This inside a method = " + testObj.check() + "<br>"; sampleEle.innerHTML += "This without any scope = " + thisRef + "<br>"; }, false ); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the “CLICK HERE” button −
- Related Articles
- How does the “this” keyword work in JavaScript?
- This keyword in Java
- ‘this’ keyword in C#
- This keyword in Dart Programming
- Explain Keyword driven framework.
- How to access 'this' keyword inside an arrow function in JavaScript?
- Using EXPLAIN keyword in MySQL
- Fetch info with MySQL EXPLAIN KEYWORD?
- Super keyword in JavaScript?
- Class Keyword in JavaScript
- "extends" keyword in JavaScript?
- How to work with this keyword in Java?
- What are the uses of this keyword in java?
- Can we call methods using this keyword in java?
- What is the use of this keyword in TypeScript?

Advertisements