Bird
Raised Fist0
Javascriptprogramming~5 mins

What this keyword represents in Javascript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the this keyword represent in JavaScript?

The this keyword refers to the object that is currently executing the code. It changes depending on how a function is called.

Click to reveal answer
intermediate
In a regular function, what does this refer to in strict mode?

In strict mode, this inside a regular function is undefined. Without strict mode, it refers to the global object (like window in browsers).

Click to reveal answer
intermediate
How does this behave inside an arrow function?

Arrow functions do not have their own this. They inherit this from the surrounding (lexical) scope.

Click to reveal answer
beginner
What does this refer to when used inside an object method?

Inside an object method, this refers to the object that owns the method.

Click to reveal answer
intermediate
How can you explicitly set the value of this when calling a function?

You can use call(), apply(), or bind() methods to explicitly set this when calling a function.

Click to reveal answer
What does this refer to inside a regular function called in the global scope (non-strict mode)?
AThe function itself
BThe object that called the function
CUndefined
DThe global object (e.g., window in browsers)
Inside an arrow function, this is:
ABound to the arrow function itself
BInherited from the surrounding scope
CAlways the global object
DUndefined
Which method can be used to permanently set this for a function?
Abind()
Bapply()
Ccall()
DsetThis()
In an object method, this refers to:
AUndefined
BThe global object
CThe object owning the method
DThe method itself
What is the value of this inside a function in strict mode if called without an object?
AUndefined
BThe function itself
CThe last object used
DThe global object
Explain how the value of this changes depending on how a function is called in JavaScript.
Think about different ways to call functions and how this behaves.
You got /4 concepts.
    Describe the difference between this in regular functions and arrow functions.
    Focus on how this is determined inside each function type.
    You got /3 concepts.

      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