JavaScript Tutorial
JavaScript this keyword (Ways to Use with Example)
Table of Contents
- Introduction
- What is the this Keyword in JavaScript?
- Ways to Use this Keyword in JavaScript
- this in Event Handlers
- Why Use this Keyword in JavaScript
JavaScript this Keyword - FAQs
In browsers, it refers to the window object. In strict mode, it is undefined.
Yes, by using methods like call(), apply(), or bind().
Arrow functions inherit this from their enclosing context.
Regular functions determine this based on the invocation context, whereas arrow functions inherit this from their lexical scope.
In nested functions, this defaults to the global object unless explicitly bound or handled with techniques like storing this in a variable or using arrow functions.
You can change the value of this using bind(), call(), or apply().
In strict mode, this is undefined when used in a regular function called in the global context. This prevents unintended reference to the global object.
In regular functions, this refers to the element that triggered the event. In arrow functions, it inherits this from the enclosing scope.
No, this always refers to a single object depending on the invocation context at the time of execution.