this Questions with Answers

14 practice questions on this (JavaScript) with answers and step-by-step solutions. Try 10 samples below, then practise all 14 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

const obj = { n: 5, get() { return this.n; } };
console.log(obj.get.call({ n: 9 }));
  1. 5
  2. undefined
  3. 9
  4. TypeError

Answer: C. 9

Explanation

call sets this to the object passed in, so this.n is 9.

Step-by-step solution

  1. call overrides the receiver.
  2. this.n becomes 9.

Why the other options are wrong

  • A. call replaces this with the given object.
  • B. The supplied object has n.
  • D. get is an ordinary function value.

Question 2Easy

What is the output?

const user = {
  name: "Asha",
  greet() { return "Hi " + this.name; }
};
console.log(user.greet());
  1. Hi null
  2. Hi undefined
  3. TypeError
  4. Hi Asha

Answer: D. Hi Asha

Explanation

When a function is called as a method (obj.method()), this is the object before the dot.

Step-by-step solution

  1. When a function is called as a method using the dot syntax (object.method()), JavaScript sets this inside that function to the object before the dot.
  2. Here, user.greet() means this inside greet refers to user.
  3. So this.name resolves to user.name, which is "Asha", giving the final string "Hi Asha".

Why the other options are wrong

  • A. "Hi null" would require this.name to somehow be null, but this correctly refers to user, whose name property is set to "Asha".
  • B. "Hi undefined" would occur only if this didn't correctly point to user (for example, if greet were called without the object context), but calling it as user.greet() does provide that context.
  • C. A TypeError isn't triggered here; this.name is simply a normal property access on a valid object.

Tip: Ask 'what's immediately before the dot when this function is called?' - that's usually what this refers to for a regular (non-arrow) function.

Question 3Easy

What is the output?

function Point(x) {
  this.x = x;
}
const p = new Point(5);
console.log(p.x);
  1. undefined
  2. null
  3. TypeError
  4. 5

Answer: D. 5

Explanation

With new, a fresh object is created and bound to this inside the constructor, and that object is returned.

Step-by-step solution

  1. The new keyword changes how a function call works in several specific ways.
  2. It creates a brand new, empty object, and sets this inside the constructor function to refer to that new object.
  3. Inside Point, this.x = x; assigns the passed-in value (5) as a property on that new object, which is then returned automatically by new, so p.x is 5.

Why the other options are wrong

  • A. undefined would suggest this.x was never actually set, but the constructor body explicitly runs this.x = x with x being 5.
  • B. null would be a value you'd have to explicitly assign; nothing in this code assigns null to x.
  • C. A TypeError isn't triggered; Point is called correctly with new and a valid argument, so the constructor runs normally.

Question 4Easy

What is the output?

const user = {
  name: 'Asha',
  hi() { return this.name; }
};
console.log(user.hi());
  1. Asha
  2. undefined
  3. user
  4. TypeError

Answer: A. Asha

Explanation

In a method call this is the object to the left of the dot, so this.name is "Asha".

Step-by-step solution

  1. user.hi() sets this to user.
  2. this.name is "Asha".

Why the other options are wrong

  • B. this is the object before the dot.
  • C. this.name is the property, not the object.
  • D. hi is a normal method.

Question 5Medium

What is the output?

const obj = {
  name: "A",
  run() {
    const inner = () => this.name;
    return inner();
  }
};
console.log(obj.run());
  1. A
  2. TypeError
  3. undefined
  4. ""

Answer: A. A

Explanation

Arrow functions do not have their own this. inner uses the this of run, which is obj.

Step-by-step solution

  1. Arrow functions are special in that they do NOT get their own this binding; instead, they capture this from whatever scope lexically surrounds them when they are defined.
  2. Here, inner is an arrow function defined inside run(), so it captures run's this, not some separate, independent this of its own.
  3. Since run() is called as obj.run(), this inside run (and therefore inside inner too) refers to obj, so inner() returns obj.name, which is "A".

Why the other options are wrong

  • B. A TypeError isn't triggered; name is a normal, existing property being accessed on a valid object (obj, via the inherited this).
  • C. undefined would occur if this inside inner somehow lost its connection to obj, but arrow functions specifically preserve (rather than reset) this from their surrounding scope.
  • D. An empty string "" is not a default fallback JavaScript produces here; obj.name has an actual, explicit value, "A".

Tip: Arrow function this = 'whatever this was in the surrounding code where the arrow function was written', fixed permanently at definition time.

Question 6Medium

What is the difference between call and apply?

  1. apply returns a new bound function, while call invokes the original function right away
  2. There is no difference; apply is simply an older alias kept for compatibility
  3. call invokes the function later, while apply invokes it immediately with the given this
  4. call takes arguments individually; apply takes them as an array

Answer: D. call takes arguments individually; apply takes them as an array

Explanation

Both invoke the function immediately with a given this. fn.call(ctx, a, b) passes arguments individually and fn.apply(ctx, [a, b]) passes them as an array. bind returns a new function instead.

Step-by-step solution

  1. Both call and apply let you invoke a function immediately while explicitly specifying what this should be inside it.
  2. The key difference between them is how you pass the function's own arguments.
  3. call takes the extra arguments individually, one after another (fn.call(ctx, a, b)), while apply takes them bundled together as a single array (fn.apply(ctx, [a, b])).

Why the other options are wrong

  • A. This description actually matches bind, not apply; bind returns a new function for later use instead of invoking immediately.
  • B. There IS a real difference between call and apply (how arguments are passed), so saying there's none is incorrect.
  • C. This swaps the timing description; in fact, BOTH call and apply invoke the function immediately - bind is the one that delays invocation.

Tip: Remember 'apply' takes an Array of arguments (both start with 'A'), while 'call' takes a Comma-separated list.

Question 7Medium

What is the output?

const o = {
  v: 1,
  reg() { return [1].map(function () { return this === undefined; })[0]; },
  arr() { return [1].map(() => this.v)[0]; }
};
console.log(o.arr());
  1. undefined
  2. true
  3. null
  4. 1

Answer: D. 1

Explanation

The arrow function inside arr takes this from arr, which is o, so this.v is 1.

Step-by-step solution

  1. arr is called as o.arr(), so this = o.
  2. The arrow inherits that this.
  3. this.v is 1.

Why the other options are wrong

  • A. The arrow inherits this from arr.
  • B. That is the check from reg, not arr.
  • C. The arrow reads this.v, which is 1.

Question 8Medium

What is the output?

const user = {
  name: 'Asha',
  hi() { 'use strict'; return this; }
};
const f = user.hi;
console.log(f());
  1. undefined
  2. Asha
  3. user
  4. globalThis

Answer: A. undefined

Explanation

Calling f() with no object in strict mode gives this = undefined.

Step-by-step solution

  1. f is called without a receiver.
  2. Strict mode does not substitute globalThis.
  3. this is undefined.

Why the other options are wrong

  • B. A detached function has no receiver.
  • C. The object is lost when the method is copied out.
  • D. In strict mode a plain call leaves this undefined.

Tip: Use bind or an arrow wrapper to keep the receiver.

Question 9Hard

What is the output?

class Counter {
  value = 0;
  inc() { return this; }
}
const inc = new Counter().inc;
console.log(inc());
  1. undefined
  2. TypeError
  3. Counter { value: 0 }
  4. globalThis

Answer: A. undefined

Explanation

Class bodies are always in strict mode. Calling the method detached from its object gives it no receiver, so this is undefined.

Step-by-step solution

  1. Methods on a class body are always run in strict mode, by default, regardless of whether the surrounding code has 'use strict'.
  2. Here, inc is extracted from its object (const inc = new Counter().inc;) and then called standalone, with no object before a dot.
  3. In strict mode, calling a function with no explicit receiver sets this to undefined (rather than defaulting to the global object, as happens in non-strict mode), so inc() returns undefined.

Why the other options are wrong

  • B. A TypeError isn't triggered simply by calling inc() this way; this being undefined inside inc is fine, since inc's body just returns this without trying to access any property on it.
  • C. Counter { value: 0 } would be the result only if this still correctly pointed to the original instance, but detaching the method from its object loses that connection entirely.
  • D. globalThis would be the this value in NON-strict mode when a function is called with no receiver, but class bodies are always strict mode, so this stays undefined instead.

Tip: To safely pass a class method as a callback without losing this, bind it explicitly (this.inc = this.inc.bind(this)) or use an arrow-function class field.

Question 10Hard

What is the output?

function getX() { return this.x; }
const bound = getX.bind({ x: 1 });
console.log(bound.call({ x: 2 }));
  1. 2
  2. TypeError
  3. 1
  4. undefined

Answer: C. 1

Explanation

A bound function's this is fixed permanently. call and apply cannot override it (only new can).

Step-by-step solution

  1. bind() creates a new function whose this value is permanently locked to whatever was passed to bind, regardless of how that new function is later called.
  2. Here, getX.bind({ x: 1 }) creates bound, a new function permanently tied to { x: 1 } as its this.
  3. Calling bound.call({ x: 2 }) tries to override this with { x: 2 }, but a bound function's this cannot be overridden by call, apply, or even another bind - so it still uses { x: 1 }, returning 1.

Why the other options are wrong

  • A. 2 would be the result if call's attempt to override this actually worked, but bound functions are specifically designed to ignore such later attempts to change this.
  • B. A TypeError isn't triggered; calling a bound function with call is perfectly valid syntax, it just has no effect on this.
  • D. undefined would require the bound this ({ x: 1 }) to somehow not have an x property, but it clearly does.

Tip: Once a function is created via .bind(), no later call/apply can change its this - only calling it with new can override a bound this.

Practise all 14 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all JavaScript topics · All topics