
A Comprehensive Guide for Beginners
If you've ever found yourself confused by the this keyword in JavaScript, you're not alone. It can be one of the trickiest concepts to grasp, especially since its behavior changes depending on the context. But fear not! By the end of this guide, you'll have a clear understanding of how this works and how to use it effectively.
What is this in JavaScript?
In JavaScript, this is a special keyword that refers to the object it belongs to. Its value is determined by how a function is called. Let's break it down with a series of examples, starting simple and getting progressively more complex.
Basic Uses of this
Global Context When
thisis used in the global context (outside of any function or object), it points to the global object. In a browser, that'swindow; in Node.js, it'sglobal.console.log(this); // In Node.js, it will output the global object.Object Method When
thisis used inside a method of an object, it points to the object itself.const car = { brand: 'Toyota', getBrand: function() { console.log(this.brand); } }; car.getBrand(); // Output: ToyotaConstructor Function When used in a constructor function,
thispoints to the new instance of the object created by the constructor.function Animal(type) { this.type = type; } const animal1 = new Animal('Dog'); console.log(animal1.type); // Output: Dog
Getting More Complex
Losing
thisContextIf you extract a method from an object and call it,
thismight not behave as you expect.const user = { name: 'John', greet: function() { console.log(this.name); } }; const greet = user.greet; greet(); // Output: undefined or error (in strict mode)Here,
thisdoesn't point touseranymore becausegreetis called as a standalone function.Arrow Functions Arrow functions don't have their own
this. They inheritthisfrom the surrounding context at the time they are created.const team = { name: 'Developers', members: ['Alice', 'Bob'], getTeamName: function() { const showTeamName = () => { console.log(this.name); }; showTeamName(); } }; team.getTeamName(); // Output: DevelopersIn this example, the arrow function inside
getTeamNameinheritsthisfromgetTeamName's context, which isteam.thisin Callbacks Callbacks, especially those used with event listeners or asynchronous operations, can change the value ofthis.const button = { text: 'Click me', click: function() { setTimeout(function() { console.log(this.text); }, 1000); } }; button.click(); // Output: undefined (after 1 second)Here,
thisinside thesetTimeoutfunction refers to the global object, notbutton.Using
bind,call, andapplyYou can explicitly set the value of
thisusingbind,call, orapply.const laptop = { brand: 'Apple', getBrand: function() { console.log(this.brand); } }; const tablet = { brand: 'Samsung' }; laptop.getBrand.call(tablet); // Output: SamsungThe
callmethod allows you to setthistotabletfor thegetBrandmethod.thisin Class MethodsWhen using classes,
thisbehaves consistently with how it works in constructor functions and object methods.class Person { constructor(name) { this.name = name; } sayName() { console.log(this.name); } } const person1 = new Person('Alice'); person1.sayName(); // Output: Alice
Advanced Example: Combining Concepts
Let's put everything together into a more complex scenario involving different contexts, arrow functions, and bind.
class Robot {
constructor(name) {
this.name = name;
this.friends = [];
}
addFriend(friend) {
this.friends.push(friend);
return this; // for chaining
}
greetFriends() {
this.friends.forEach(function(friend) {
console.log(this.name + ' greets ' + friend);
}.bind(this)); // binding `this` to the Robot instance
}
greetFriendsArrow() {
this.friends.forEach((friend) => {
console.log(this.name + ' greets ' + friend);
});
}
}
const robot1 = new Robot('Robo');
robot1.addFriend('Alice').addFriend('Bob').greetFriends();
// Output:
// Robo greets Alice
// Robo greets Bob
robot1.greetFriendsArrow();
// Output:
// Robo greets Alice
// Robo greets Bob
In this example:
addFriendmethod usesthisto refer to the instance ofRobot.greetFriendsmethod usesbindto ensurethisinside theforEachcallback refers to theRobotinstance.greetFriendsArrowmethod uses an arrow function to keep thethiscontext of theRobotinstance.
Summary
thisin the global context refers to the global object.In a method,
thisrefers to the object owning the method.In a constructor,
thisrefers to the new instance being created.Arrow functions inherit
thisfrom the surrounding lexical context.Use
bind,call, andapplyto explicitly setthis.Context can change in callbacks and asynchronous operations, often requiring
bindor arrow functions to maintain the correctthis.
Mastering this is crucial for writing effective and bug-free JavaScript, especially as you dive into more advanced and complex code. With these examples and explanations, you should have a solid foundation to understand and utilize this in your JavaScript projects.
About the author
Building Enterprise Softwares🚀 | Sharing insights on Linkedin/in/Waleed-javed
Comments
Join the discussionNo comments yet. Be the first to comment.





