- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to access 'this' keyword inside an arrow function in JavaScript?
"this" keyword in an arrow function
The JavaScript 'this' keyword refers to the object it belongs to. In an arrow function, 'this' belongs to a global object. Inside a simple function, there might be chances that 'this' keyword can result in undefined but in an arrow function it results in an exact value.
Example
<html> <body> <script> function Student(fname, grade) { this.fname = fname; this.grade = grade; this.details = function() { return () => { document.write(`Hi, I'm ${this.fname} from ${this.grade} grade`); }; } } let info = new Student('picaso', 'seventh'); let printInfo = info.details(); printInfo(); </script> </body> </html>
Output
Hi, I'm picaso from seventh grade
- Related Articles
- What is the 'new' keyword in JavaScript to create an object and how to access values?
- How to use 'const' keyword in JavaScript?
- How to use the 'with' keyword in JavaScript?
- What is the 'new' keyword in JavaScript?
- How to use the 'and' keyword in Ruby?
- 'abstract' keyword in Java
- How to invoke a JavaScript Function with 'new' Function Constructor?
- Access property as a property using 'get' in JavaScript?
- How to define an Arrow Function in JavaScript?
- When should I write the keyword 'inline' for a function/method in C++?
- How to make a condition for event 'click' inside/outside for multiple divs - JavaScript?
- What does 'by' keyword do in Kotlin?
- Explain 'Not a constructor function' error in JavaScript?
- How to click the 'Ok' button inside an alert window with a Selenium command?
- 'this' reference in Java

Advertisements