0
0
Javascriptprogramming~15 mins

What this keyword represents in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use console.log(brandName); to print the brand.