Bird
Raised Fist0
Javascriptprogramming~5 mins

What this keyword represents in Javascript

Choose your learning style10 modes available
Introduction

The this keyword helps you access the current object or context inside a function or method.

When you want to refer to the object that owns the current code.
When you write methods inside objects and want to access other properties of the same object.
When you handle events and want to know which element triggered the event.
When you use constructor functions or classes to create objects and want to set properties.
When you want to write reusable code that works with different objects.
Syntax
Javascript
function example() {
  console.log(this);
}

this value depends on how the function is called, not where it is defined.

In global code, this usually refers to the global object (like window in browsers).

Examples
this refers to obj, so it prints 'Alice'.
Javascript
const obj = {
  name: 'Alice',
  greet() {
    console.log(this.name);
  }
};
obj.greet();
In non-strict mode, this is the global object. In strict mode, it is undefined.
Javascript
function show() {
  console.log(this);
}
show();
this inside the class refers to the instance p.
Javascript
class Person {
  constructor(name) {
    this.name = name;
  }
  sayName() {
    console.log(this.name);
  }
}
const p = new Person('Bob');
p.sayName();
Sample Program

This program shows how this refers to the car object inside the method.

Javascript
const car = {
  brand: 'Toyota',
  showBrand() {
    console.log(`This car is a ${this.brand}`);
  }
};
car.showBrand();
OutputSuccess
Important Notes

Arrow functions do not have their own this; they use this from the surrounding code.

Be careful when extracting methods from objects; this may lose its original meaning.

Summary

this points to the current object or context.

Its value depends on how a function is called.

It helps write flexible and reusable code.

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