Bird
Raised Fist0
Javascriptprogramming~10 mins

What this keyword represents in Javascript - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to log the current object using this keyword.

Javascript
const obj = { name: 'Alice', show: function() { console.log([1]); } }; obj.show();
Drag options to blanks, or click blank then click option'
Aself
Bobj
Cthis
Dwindow
Attempts:
3 left
💡 Hint
Common Mistakes
Using the object name directly instead of this inside the method.
Using window which refers to the global object, not the current object.
2fill in blank
medium

Complete the code to correctly bind this inside the function.

Javascript
function greet() { console.log('Hello, ' + this.name); } const person = { name: 'Bob' }; const greetPerson = greet.[1](person); greetPerson();
Drag options to blanks, or click blank then click option'
Abind
Bcall
Capply
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using call or apply which invoke the function immediately.
Using a non-existent method like set.
3fill in blank
hard

Fix the error by completing the code to access this inside the arrow function.

Javascript
const obj = { name: 'Carol', greet: function() { const arrow = () => console.log(this.[1]); arrow(); } }; obj.greet();
Drag options to blanks, or click blank then click option'
Aobj
Bthis
Cgreet
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use this as a property name.
Using obj directly inside the arrow function.
4fill in blank
hard

Fill both blanks to create a method that returns the object's name using this.

Javascript
const user = { [1]: function() { return this.[2]; }, name: 'Dave' }; console.log(user.getName());
Drag options to blanks, or click blank then click option'
AgetName
Bname
Cusername
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong method name that does not match the call.
Accessing a property that does not exist like username.
5fill in blank
hard

Fill all three blanks to create an object with a method that uses this to return a greeting.

Javascript
const person = { [1]: 'Eve', [2]: function() { return `Hello, my name is [3].`; } }; console.log(person.greet());
Drag options to blanks, or click blank then click option'
Aname
Bgreet
Cthis.name
Dthis.greet
Attempts:
3 left
💡 Hint
Common Mistakes
Using this.greet inside the template string instead of this.name.
Mixing up property and method names.

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

  1. Step 1: Understand method call context

    When a function is called as a method of an object, this refers to that object.
  2. Step 2: Apply to example

    Inside the method, this points to the object owning the method, not the global object or function itself.
  3. Final Answer:

    The object that owns the method -> Option A
  4. 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

  1. Step 1: Understand arrow function this behavior

    Arrow functions do not have their own this; they inherit it from the surrounding scope.
  2. Step 2: Apply to object method

    Using this inside an arrow function refers to the this of the outer function or object context.
  3. Final Answer:

    Arrow functions do not have their own this, so it inherits from the surrounding scope -> Option D
  4. 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
3. Consider the code:
const obj = {
  name: 'Alice',
  greet() {
    console.log(this.name);
  }
};
const greetFunc = obj.greet;
greetFunc();

What will be the output when greetFunc() is called?
medium
A. Alice
B. undefined
C. Error
D. null

Solution

  1. Step 1: Understand function reference assignment

    Assigning obj.greet to greetFunc detaches the method from its object.
  2. Step 2: Call context of greetFunc()

    When called alone, this inside greetFunc is the global object or undefined in strict mode, so this.name is undefined.
  3. Final Answer:

    undefined -> Option B
  4. Quick Check:

    Detached method this = undefined [OK]
Hint: Detached methods lose their object this [OK]
Common Mistakes:
  • Expecting this to still point to the object
  • Thinking it prints the name property
  • Ignoring strict mode effects on this
4. Identify the error in the following code:
const person = {
  name: 'Bob',
  sayName: () => {
    console.log(this.name);
  }
};
person.sayName();
medium
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

  1. 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.
  2. Step 2: Effect on console.log(this.name)

    Since this is not person, this.name is undefined or from global scope, not 'Bob'.
  3. Final Answer:

    this inside arrow function does not refer to person -> Option A
  4. 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

  1. Step 1: Understand constructor function usage

    Constructor functions use this to assign properties to the new object created by new.
  2. 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.
  3. Final Answer:

    function Car(make) { this.make = make; } const myCar = new Car('Toyota'); -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard