Discover how a tiny word like <code>this</code> can make your code smarter and less confusing!
Why What this keyword represents in Javascript? - Purpose & Use Cases
Imagine you have a group of friends, and you want to tell each one their own favorite color. You try to write a note for each friend, but you keep mixing up who likes what color because you don't have a clear way to say "this friend" in your notes.
Without a clear way to refer to the current friend, you might write confusing notes or repeat the same color for everyone. This makes your messages slow to write and easy to mess up, especially if you have many friends.
The this keyword in JavaScript acts like a pointer to the current object you are working with. It helps you say "this particular friend" clearly, so you can easily access their favorite color or other details without confusion.
function showColor(friend) {
console.log(friend.name + ' likes ' + friend.color);
}
showColor({name: 'Alice', color: 'blue'});const friend = {
name: 'Alice',
color: 'blue',
showColor() {
console.log(this.name + ' likes ' + this.color);
}
};
friend.showColor();Using this lets you write cleaner, reusable code that automatically knows which object it belongs to, making your programs easier to understand and maintain.
Think of a car with many buttons. Each button controls a specific feature of that car. The this keyword is like the label on each button that tells it which car it belongs to, so pressing it affects the right car.
this points to the current object in context.
It helps avoid confusion when accessing object properties.
Using this makes code cleaner and easier to reuse.
Practice
this keyword represent inside a regular function in JavaScript when called as a method of an object?Solution
Step 1: Understand method call context
When a function is called as a method of an object,thisrefers to that object.Step 2: Apply to example
Inside the method,thispoints to the object owning the method, not the global object or function itself.Final Answer:
The object that owns the method -> Option AQuick Check:
thisin method = owning object [OK]
this is the object before the dot [OK]- Thinking
thisalways points to global object - Confusing
thiswith the function itself - Assuming
thisis undefined in methods
this inside an arrow function to refer to the surrounding object?Solution
Step 1: Understand arrow function
Arrow functions do not have their ownthisbehaviorthis; they inherit it from the surrounding scope.Step 2: Apply to object method
Usingthisinside an arrow function refers to thethisof the outer function or object context.Final Answer:
Arrow functions do not have their ownthis, so it inherits from the surrounding scope -> Option DQuick Check:
Arrow functionthis= inherited context [OK]
this from outside [OK]- Assuming arrow functions have their own
this - Using arrow functions as object methods expecting
thisto be the object - Confusing arrow function
thiswith regular functionthis
const obj = {
name: 'Alice',
greet() {
console.log(this.name);
}
};
const greetFunc = obj.greet;
greetFunc();What will be the output when
greetFunc() is called?Solution
Step 1: Understand function reference assignment
Assigningobj.greettogreetFuncdetaches the method from its object.Step 2: Call context of
When called alone,greetFunc()thisinsidegreetFuncis the global object or undefined in strict mode, sothis.nameis undefined.Final Answer:
undefined -> Option BQuick Check:
Detached methodthis= undefined [OK]
this [OK]- Expecting
thisto still point to the object - Thinking it prints the name property
- Ignoring strict mode effects on
this
const person = {
name: 'Bob',
sayName: () => {
console.log(this.name);
}
};
person.sayName();Solution
Step 1: Analyze arrow function
Arrow functions do not have their ownthisbehaviorthis; they inherit from the outer scope, which is notperson.Step 2: Effect on
Sinceconsole.log(this.name)thisis notperson,this.nameis undefined or from global scope, not 'Bob'.Final Answer:
thisinside arrow function does not refer toperson-> Option AQuick Check:
Arrow functionthis≠ object [OK]
this [OK]- Thinking arrow functions bind
thisto object - Assuming syntax error due to arrow function
- Confusing missing parentheses with
thisissues
Car that sets the make property using this. Which code correctly uses this to assign the property when creating a new car?Solution
Step 1: Understand constructor function usage
Constructor functions usethisto assign properties to the new object created bynew.Step 2: Check each option
function Car(make) { this.make = make; } const myCar = new Car('Toyota'); correctly assignsthis.make = makeand usesnewto create an object. function Car(make) { make = this; } const myCar = Car('Toyota'); assigns incorrectly. const Car = (make) => { this.make = make; }; const myCar = new Car('Toyota'); uses arrow function which does not bindthis. function Car(make) { this.make = make; } const myCar = Car('Toyota'); calls constructor withoutnew, sothisis not the new object.Final Answer:
function Car(make) { this.make = make; } const myCar = new Car('Toyota'); -> Option CQuick Check:
Constructorthisneedsnew[OK]
new with constructor to bind this correctly [OK]- Calling constructor without
new - Using arrow functions as constructors
- Assigning
make = thisinstead ofthis.make = make
Ready to prove it?
Pass the test and this pattern moves unknown
