Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is the this keyword in JavaScript

Demystifying the 'this' keyword in JavaScript

If you're learning programming, especially JavaScript, you may have come across the keyword 'this'. It can be a bit bewildering at first, but don't worry, we're going to unravel its mysteries together.

What is 'this'?

Simply put, 'this' is a reference keyword in JavaScript. It refers to the context in which a function is called or an object it belongs to. It might sound a bit cryptic now, but let's break it down with a code example.

let MyObject = function (name) {
    this.name = name;
};

let obj = new MyObject('My Object');
console.log(obj.name); // Outputs: 'My Object'

In this example, 'this' is used within the function MyObject to refer to the new object being created. So this.name is essentially the name property of the new object.

How 'this' changes based on context

One peculiar thing about 'this' is that it changes based on the context. This doesn't happen in all programming languages, making it a unique feature (or quirk, depending on how you see it) of JavaScript.

let MyObject = function (name) {
    this.name = name;
};

MyObject.prototype.sayName = function () {
    console.log(this.name);
};

let obj1 = new MyObject('Object 1');
let obj2 = new MyObject('Object 2');

obj1.sayName(); // Outputs: 'Object 1'
obj2.sayName(); // Outputs: 'Object 2'

In this example, we added a method sayName to MyObject using the prototype. When we call sayName from different objects, 'this' refers to the object from which it was called.

'this' in event handlers

Now, let's see how 'this' behaves in event handlers. In JavaScript, event handlers are functions that respond to certain actions like clicking a button or loading a page.

let button = document.querySelector('button');
button.addEventListener('click', function() {
    this.classList.toggle('active');
});

Here, 'this' refers to the object that the event is acting on, which is the button being clicked. When the button is clicked, the 'active' class is either added or removed from the button.

'this' in Arrow Functions

Arrow functions handle 'this' a bit differently. Instead of having their own 'this', they inherit 'this' from the parent scope.

let MyObject = function (name) {
    this.name = name;
    this.sayName = () => {
        console.log(this.name);
    };
};

let obj = new MyObject('My Name');
obj.sayName(); // Outputs: 'My Name'

In this example, even though sayName is an arrow function, 'this' still correctly refers to obj.

Conclusion - The Shape-shifting 'this'

In a world of constants, 'this' is a variable. It's a shape-shifter, changing what it represents based on where it finds itself. It's not a villain, though it may initially come across as a tricky character. Once you understand its rules, you can harness its power and bring your JavaScript code to life.

Remember, 'this' is not an enemy, but a tool at your disposal. It's all about understanding and using it in the right context. Like a chameleon, it adapts to its surroundings, and it's this adaptability that makes 'this' one of the most dynamic and versatile aspects of JavaScript.

So the next time you encounter 'this', don't panic. Take a step back, understand the context, and you'll be able to predict its behavior. From event handlers to arrow functions, 'this' shows us that sometimes, being inconstant can be a good thing. It's all about perspective. Happy Coding!