The this
keyword is used in JavaScript to refer to the object it belongs to. It provides a way to access object properties or methods dynamically, depending on the context where it is used.
Think of a tour guide leading different groups through a museum. When the guide says, “I will show you the next exhibit,” the “I” always refers to the guide currently speaking, regardless of which group they are leading. Similarly, this
keyword dynamically refers to the object currently “leading” the operation in JavaScript. Its meaning changes based on where and how it’s used, just like how the guide’s “I” depends on the tour context.
this
keyword mean?this
is a special keyword in JavaScript that refers to an object. But what object it refers to changes depending on the context. It can point to the global window object, the object calling a function, or something else.
Let’s look at some examples:
this
keyword in an object methodWhen this
is used inside a method (a function inside an object), it refers to the object that owns the method.
const person = {name: "Alice",greet: function () {console.log(`Hello, my name is ${this.name}`);},};person.greet(); // Output: Hello, my name is Alice
Explanation: this.name
points to the name
property of the person
object because the greet
function is called from person
.
this
keyword in the global contextWhen this
is used outside any function or object, it refers to the global object. In browsers, the global object is called window
.
console.log(this); // In browsers, this points to the window object
Explanation: Here, this
refers to the global environment, which contains properties and methods available throughout the code.
While this
points to the global object in browsers when used outside any function, its behavior changes significantly when used inside a function. Let’s see how.
this
keyword in a functionBy default, when we use this
in a regular function, it refers to the global object.
function showThis() {console.log(this);}showThis(); // Output: window (in non-strict mode)
Explanation: In non-strict mode, this
points to the global object inside functions. But if strict mode is enabled, this
will be undefined
unless explicitly set.
this
keyword in strict modeStrict mode changes how this
works. In strict mode, this
inside a function does not point to the global object. Instead, it remains undefined
unless explicitly set.
"use strict";function showThis() {console.log(this);}showThis(); // Output: undefined
Explanation: Strict mode helps prevent mistakes and makes code safer. It disables the default global binding for this
.
Note: In browsers, this
in the global scope refers to the window
object, while in Node.js, it refers to the global
object, making it environment-dependent.
this
keyword in event handlersIn event handlers, this
refers to the element that triggered the event.
document.querySelector("button").addEventListener("click", function () {console.log(this); // Refers to the button element});
Explanation: Here, when the button is clicked, this
refers to the button that caused the event. This helps to work with the specific element in code.
call
, apply
, and bind
We can explicitly set the value of this
for a function using call
, apply
, and bind
. These methods allow us to control the context (this
) in which a function is executed, which is especially useful when the default value of this
doesn’t behave as expected.
call()
The call
method invokes a function and explicitly sets this
value. It accepts this
value as its first argument, followed by individual function arguments.
function greet(greeting, punctuation) {console.log(`${greeting}, ${this.name}${punctuation}`);}const person = { name: "Alice" };greet.call(person, "Hello", "!"); // Output: Hello, Alice!
Explanation: this
inside the greet
function is explicitly set to the person
object.
apply()
The apply
method works similarly to call
, but instead of passing arguments individually, you pass them as an array.
greet.apply(person, ["Hi", "."]); // Output: Hi, Alice.
Explanation: The apply
method is particularly useful when arguments are already in an array format.
Try this: Copy the apply()
function and test it in the code editor above to observe the output.
bind()
The bind
method doesn’t immediately invoke the function. Instead, it creates a new function with the this
value permanently set to the specified object.
const greetAlice = greet.bind(person);greetAlice("Hey", "!"); // Output: Hey, Alice!
Explanation: This creates a new function greetAlice
, where this
is permanently set to the person
object.
Try this: Copy the bind()
function and test it in the code editor above to observe the output.
this
Arrow functions do not have their own this
. Instead, they take this
from the surrounding context where they are defined.
const obj = {name: "Charlie",regularFunction: function() {console.log(this.name); // `this` refers to the `obj` object},};obj.regularFunction(); // Output: "Charlie"
Explanation: In this code, the regularFunction
uses a traditional function, so this
refers to the obj
object, and thus this.name
outputs "Charlie"
.
The this
keyword refers to an object, but the object it refers to depends on the context.
In a method, this
refers to the object calling the method.
In the global context, this
refers to the global object (like a window
in browsers).
In strict mode, this
inside a function is undefined
.
Arrow functions do not have their own this
; they inherit it from the outer scope.
Ready to learn JavaScript and build interactive web applications? Start our Learn JavaScript course and gain a solid foundation, preparing you for a career in web development. Ready to become a full-stack web developer? Begin our Become a Web Developer skill path, starting with the basics and advancing to expert-level skills, setting you up for success in web development.
What does the this
keyword refer to when used in a global context in a browser environment?
The object calling the function
The window object
The undefined value
Haven’t found what you were looking for? Contact Us
Free Resources