This keyword in JavaScript arrow function | Example code

  • by

JavaScript Arrow functions have no this value in their scope, so you can access this value of the object. But Normal functions have this value in their scope.

This keyword in the JavaScript arrow function

A simple example code value of the normal function is global this or window. and it allows to you access the global scope.

<!DOCTYPE html>
<html>
<body>

  <script>
    var greeting = 'Hi';
    const obj = {
      greeting: 'Hey Arrow Function',
      fo() {
        const greeting = 'Hola';
        fo2 =  () => {
          const greeting = 'Hello';
          const arrowFo = () => {
            console.log(this.greeting);
          };
          arrowFo();
        };
        fo2();

      },
    };
    obj.fo();
  </script>

</body>
</html> 

Output:

This keyword in JavaScript arrow function

Source: stackoverflow.com

Do comment if you have any doubts or suggestions on this JS this keyword.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.