Bird
Raised Fist0
Javascriptprogramming~20 mins

What this keyword represents in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of this keyword
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of this in a regular function
What is the output of the following code when calling obj.show()?
Javascript
const obj = {
  name: 'Alice',
  show: function() {
    console.log(this.name);
  }
};
obj.show();
A"Alice"
Bundefined
CReferenceError
D"obj"
Attempts:
2 left
💡 Hint
Inside an object method, this refers to the object itself.
❓ Predict Output
intermediate
2:00remaining
Value of this in arrow function inside object
What will be logged when calling obj.show() in this code?
Javascript
const obj = {
  name: 'Bob',
  show: () => {
    console.log(this.name);
  }
};
obj.show();
A"obj"
B"Bob"
Cundefined
DReferenceError
Attempts:
2 left
💡 Hint
Arrow functions do not have their own this.
❓ Predict Output
advanced
2:00remaining
this inside event listener function
What does this refer to inside the event listener function in this code?
Javascript
const button = document.createElement('button');
button.textContent = 'Click me';
document.body.appendChild(button);

button.addEventListener('click', function() {
  console.log(this);
});
AThe button element that was clicked
BThe global window object
Cundefined
DThe document object
Attempts:
2 left
💡 Hint
In event listeners, this refers to the element the listener is attached to.
❓ Predict Output
advanced
2:00remaining
this in class method with setTimeout
What will be logged when calling obj.delayedShow()?
Javascript
class Person {
  constructor(name) {
    this.name = name;
  }
  delayedShow() {
    setTimeout(function() {
      console.log(this.name);
    }, 100);
  }
}
const obj = new Person('Carol');
obj.delayedShow();
AReferenceError
Bundefined
C"Carol"
DTypeError
Attempts:
2 left
💡 Hint
Regular functions inside setTimeout have this as the global object or undefined in strict mode.
🧠 Conceptual
expert
2:00remaining
Understanding this binding with call, apply, and bind
Given the code below, which option correctly shows the output of greet.call(person, 'Hello')?
Javascript
function greet(greeting) {
  return `${greeting}, my name is ${this.name}`;
}
const person = { name: 'Dave' };
ATypeError
B"Hello, my name is greet"
C"Hello, my name is undefined"
D"Hello, my name is Dave"
Attempts:
2 left
💡 Hint
call sets this explicitly to the first argument.

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