Understanding the <code>this</code> Keyword in JavaScript
📖 Scenario: Imagine you have a simple object representing a car. You want to create a method inside this object that can show the car's brand using the this keyword.
🎯 Goal: Build a JavaScript object with a method that uses the this keyword to access the object's own properties and print the car brand.
📋 What You'll Learn
Create an object called car with a property brand set to 'Toyota'.
Add a method called showBrand inside the car object.
Inside the showBrand method, use the this keyword to access the brand property.
Call the showBrand method to print the brand.
💡 Why This Matters
🌍 Real World
Objects with methods using <code>this</code> are everywhere in JavaScript, like in web apps to represent users, products, or UI components.
💼 Career
Understanding <code>this</code> is essential for writing clear and correct JavaScript code, a key skill for frontend and backend developers.
Progress0 / 4 steps
1
Create the car object with a brand property
Create an object called car with a property brand set to the string 'Toyota'.
Javascript
Hint
Use const car = { brand: 'Toyota' }; to create the object.
2
Add the showBrand method using this
Add a method called showBrand inside the car object. Inside this method, use the this keyword to access the brand property.
Javascript
Hint
Define showBrand() { return this.brand; } inside the object.
3
Call the showBrand method and store the result
Call the showBrand method on the car object and store the result in a variable called brandName.
Javascript
Hint
Use const brandName = car.showBrand(); to call the method.
4
Print the brand name stored in brandName
Write a console.log statement to print the value of the variable brandName.
Javascript
Hint
Use console.log(brandName); to print the brand.
Practice
(1/5)
1. What does the this keyword represent inside a regular function in JavaScript when called as a method of an object?
easy
A. The object that owns the method
B. The global object (like window in browsers)
C. The function itself
D. Undefined
Solution
Step 1: Understand method call context
When a function is called as a method of an object, this refers to that object.
Step 2: Apply to example
Inside the method, this points to the object owning the method, not the global object or function itself.
Final Answer:
The object that owns the method -> Option A
Quick Check:
this in method = owning object [OK]
Hint: In methods, this is the object before the dot [OK]
Common Mistakes:
Thinking this always points to global object
Confusing this with the function itself
Assuming this is undefined in methods
2. Which of the following is the correct way to access this inside an arrow function to refer to the surrounding object?
easy
A. Use this directly inside the arrow function
B. Use this only inside global scope
C. Use this inside a regular function instead
D. Arrow functions do not have their own this, so it inherits from the surrounding scope
Solution
Step 1: Understand arrow function this behavior
Arrow functions do not have their own this; they inherit it from the surrounding scope.
Step 2: Apply to object method
Using this inside an arrow function refers to the this of the outer function or object context.
Final Answer:
Arrow functions do not have their own this, so it inherits from the surrounding scope -> Option D
Quick Check:
Arrow function this = inherited context [OK]
Hint: Arrow functions inherit this from outside [OK]
Common Mistakes:
Assuming arrow functions have their own this
Using arrow functions as object methods expecting this to be the object
Confusing arrow function this with regular function this
A. this inside arrow function does not refer to person
B. Missing parentheses in method call
C. Syntax error due to arrow function in object
D. name property is not defined
Solution
Step 1: Analyze arrow function this behavior
Arrow functions do not have their own this; they inherit from the outer scope, which is not person.
Step 2: Effect on console.log(this.name)
Since this is not person, this.name is undefined or from global scope, not 'Bob'.
Final Answer:
this inside arrow function does not refer to person -> Option A
Quick Check:
Arrow function this ≠ object [OK]
Hint: Avoid arrow functions for object methods needing this [OK]
Common Mistakes:
Thinking arrow functions bind this to object
Assuming syntax error due to arrow function
Confusing missing parentheses with this issues
5. You want to create a constructor function Car that sets the make property using this. Which code correctly uses this to assign the property when creating a new car?
hard
A. const Car = (make) => { this.make = make; }; const myCar = new Car('Toyota');
B. function Car(make) { make = this; } const myCar = Car('Toyota');
C. function Car(make) { this.make = make; } const myCar = new Car('Toyota');
D. function Car(make) { this.make = make; } const myCar = Car('Toyota');
Solution
Step 1: Understand constructor function usage
Constructor functions use this to assign properties to the new object created by new.
Step 2: Check each option
function Car(make) { this.make = make; } const myCar = new Car('Toyota'); correctly assigns this.make = make and uses new to 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 bind this. function Car(make) { this.make = make; } const myCar = Car('Toyota'); calls constructor without new, so this is not the new object.
Final Answer:
function Car(make) { this.make = make; } const myCar = new Car('Toyota'); -> Option C
Quick Check:
Constructor this needs new [OK]
Hint: Use new with constructor to bind this correctly [OK]
Common Mistakes:
Calling constructor without new
Using arrow functions as constructors
Assigning make = this instead of this.make = make
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.