Do you know `this` Quiz 1?

April 28, 2025

1. What is this in the global context (outside any function)?

  • A) undefined
  • B) The global object (window in browsers, global in Node.js)
  • C) null
  • D) Depends on strict mode

2. What does this refer to inside a regular function (not an arrow function) called in strict mode?

  • A) The global object
  • B) undefined
  • C) The function itself
  • D) The caller object

3. What is this inside an arrow function?

  • A) Always refers to the global object
  • B) Inherits this from the enclosing lexical scope
  • C) Always undefined
  • D) Refers to the function itself

4. What will this be in an object method when called as obj.method()?

  • A) The global object
  • B) The object (obj)
  • C) undefined
  • D) The method itself

5. What happens if a method is assigned to a variable and then called?

const obj = {
  name: "Alice",
  greet: function () {
    console.log(this.name);
  },
};
const greetFunc = obj.greet;
greetFunc();
  • A) Prints "Alice"
  • B) Prints undefined (or throws an error in strict mode)
  • C) Refers to the global object
  • D) Throws a ReferenceError

6. How can you explicitly set this when calling a function?

  • A) Using bind, call, or apply
  • B) Using this = value
  • C) Using setThis()
  • D) Using function.this()

7. What does bind do?

  • A) Immediately calls the function with a fixed this
  • B) Creates a new function with a fixed this
  • C) Changes this for all future calls
  • D) Only works in strict mode

8. What will this be in an event handler (e.g., button.addEventListener('click', function() { console.log(this); }))?

  • A) The global object
  • B) The DOM element that triggered the event
  • C) undefined
  • D) The event object

9. What is this in a constructor function (called with new)?

  • A) The newly created object
  • B) The prototype of the object
  • C) The global object
  • D) undefined

10. What happens if a constructor function is called without new?

  • A) this refers to the global object (or undefined in strict mode)
  • B) It throws an error
  • C) this refers to the function itself
  • D) It still creates an object

11. What is this inside a class method?

  • A) The class itself
  • B) The instance of the class
  • C) undefined
  • D) The prototype object

12. What will this be in a nested function inside an object method?

const obj = {
  name: "Bob",
  greet: function () {
    function inner() {
      console.log(this.name);
    }
    inner();
  },
};
obj.greet();
  • A) "Bob"
  • B) undefined (or global object in non-strict mode)
  • C) The obj
  • D) The inner function

13. How can you preserve this in a nested function?

  • A) Use bind, call, or apply
  • B) Store this in a variable (e.g., const self = this)
  • C) Use an arrow function for the nested function
  • D) All of the above

14. What is this in a setTimeout callback (regular function)?

setTimeout(function () {
  console.log(this);
}, 1000);
  • A) The global object
  • B) The setTimeout function
  • C) undefined
  • D) The caller object

15. What is this in a setTimeout callback (arrow function)?

const obj = {
  name: "Charlie",
  greet: function () {
    setTimeout(() => {
      console.log(this.name);
    }, 1000);
  },
};
obj.greet();
  • A) undefined
  • B) The global object
  • C) The obj
  • D) The setTimeout function

16. What does call do?

  • A) Calls a function with a given this and arguments as an array
  • B) Calls a function with a given this and individual arguments
  • C) Binds this permanently
  • D) Only works with arrow functions

17. What does apply do?

  • A) Calls a function with a given this and arguments as an array
  • B) Calls a function with a given this and individual arguments
  • C) Binds this permanently
  • D) Only works with arrow functions

18. What is this in a module (ES6 module) at the top level?

  • A) The global object
  • B) undefined
  • C) The module exports object
  • D) null

19. What happens if this is used in an arrow function inside an object literal?

const obj = {
  name: "Dave",
  greet: () => {
    console.log(this.name);
  },
};
obj.greet();
  • A) Prints "Dave"
  • B) Prints undefined (inherits this from outer scope)
  • C) Throws an error
  • D) Refers to the obj

20. What is this in a getter/setter method?

const obj = {
  _name: "Eve",
  get name() {
    return this._name;
  },
};
console.log(obj.name);
  • A) The global object
  • B) The obj
  • C) undefined
  • D) The getter function

Sometimes a quiz is quick way to test our understanding of a concept. This is the first of a series of quizzes on this in JavaScript.