Autocoder

JavaScript this Keyword Interview Questions

Last updated 2026-11-19

The value of this is decided by how a function is called, not where it is written, and that single fact generates a large share of JavaScript interview puzzles. Interviewers show a method, pass it somewhere, and ask what it prints.

This page lists the binding rules in priority order, then works through the standard traps: a method detached from its object, a callback inside a method, arrow functions, constructors and event handlers. Where results differ between sloppy and strict mode, both are stated.

LevelFresher to 4 years
FocusBinding rules, call/apply/bind, arrow functions
RuntimeBrowser and Node.js

The four rules and the exception

To find this, look at the call site and apply, in order: new binding, explicit binding with call, apply or bind, implicit binding when called as obj.method(), and default binding otherwise. Arrow functions are the exception: they have no this of their own and use the one from the enclosing scope.

Related topics

Arrow function scope ties into closures and ES6 features. General preparation is covered in how to crack a JavaScript interview.

JavaScript this Keyword Interview Questions: questions with worked answers

  1. 1

    What is this in JavaScript?

    easy

    this is a keyword that refers to the object a function is currently executing on behalf of. Its value is set at call time by the call site. In a method call it is the object before the dot, in a plain function call it is the global object or undefined in strict mode, in a constructor call it is the new object, and in an arrow function it is inherited lexically. At the top level of a browser script it is window, and in an ES module it is undefined.

  2. 2

    Predict the output: a method detached from its object.

    medium

    o.f() has an object before the dot, so this is o. Assigning o.f to g and calling g() has no object, so default binding applies: the global object in sloppy mode, where name is undefined, and undefined in strict mode, where reading name throws a TypeError. Passing a method as a callback behaves like g.

    javascript
    const o = { name: "A", f() { return this.name; } };
    const g = o.f;
    console.log(o.f());      // A
    console.log(g());        // undefined (TypeError in strict mode)
    setTimeout(o.f, 0);      // this is not o inside the callback
  3. 3

    How do call, apply and bind differ?

    medium

    All three set this explicitly. call(thisArg, a, b) invokes immediately with arguments listed. apply(thisArg, [a, b]) invokes immediately with an array. bind(thisArg, a) returns a new function with this and leading arguments fixed, to be called later. Spread has made apply less necessary.

    javascript
    function intro(greet, p) { return `${greet} ${this.name}${p}`; }
    const u = { name: "Asha" };
    console.log(intro.call(u, "Hi", "!"));        // Hi Asha!
    console.log(intro.apply(u, ["Hello", "?"]));  // Hello Asha?
    const bound = intro.bind(u, "Hey");
    console.log(bound("."));                      // Hey Asha.
  4. 4

    Predict the output: what happens when you bind twice, or call a bound function with call?

    hard

    A bound function's this is fixed permanently by the first bind. Later bind or call cannot override it, though extra arguments can still be added. The one exception is that new on a bound function ignores the bound this and creates a fresh object.

    javascript
    function who() { return this.n; }
    const a = who.bind({ n: "first" });
    const b = a.bind({ n: "second" });
    console.log(b());                      // first
    console.log(a.call({ n: "third" }));   // first
  5. 5

    How do arrow functions treat this?

    medium

    An arrow function has no own this, arguments or super. It captures this from the scope where it was defined, and call, apply and bind cannot change it. That makes arrows ideal for callbacks inside methods, but wrong as object methods, because the enclosing scope is not the object.

    javascript
    const o = {
      n: "obj",
      arrowProp: () => typeof this,      // outer this, not o
      method() { return [1].map(() => this.n); },
    };
    console.log(o.method());          // [ 'obj' ]
    console.log(o.arrowProp());       // object (module.exports in CommonJS)
  6. 6

    Predict the output: a callback inside a method.

    hard

    A normal function used as a callback is called without an object, so it gets default binding and loses the outer this. Solutions are an arrow function, saving this in a variable, bind, or the thisArg parameter of methods like forEach.

    javascript
    const team = {
      name: "T",
      members: ["a", "b"],
      list() {
        return this.members.map(function (m) { return `${m}-${this && this.name}`; });
      },
      listArrow() { return this.members.map((m) => `${m}-${this.name}`); },
    };
    console.log(team.list());        // [ 'a-undefined', 'b-undefined' ]
    console.log(team.listArrow());   // [ 'a-T', 'b-T' ]
  7. 7

    What does this refer to inside a constructor, and what does new do?

    medium

    new creates an empty object whose prototype is the function's prototype property, calls the function with this set to that object, and returns the object unless the function explicitly returns a different object. Returning a primitive is ignored. Forgetting new in sloppy mode makes this the global object and pollutes it.

    javascript
    function Person(name) {
      this.name = name;
    }
    const p = new Person("Ravi");
    console.log(p.name, p instanceof Person);   // Ravi true
    
    function Weird() { this.a = 1; return { b: 2 }; }
    console.log(new Weird());                   // { b: 2 }
  8. 8

    Predict the output: the comma operator and parenthesised method calls.

    hard

    A reference to obj.method keeps its base object only when called directly. Putting the reference in parentheses keeps it, but passing it through the comma operator or an assignment evaluates it to a plain function value first, so the call loses its object.

    javascript
    const o = { n: "o", f() { return this && this.n; } };
    console.log((o.f)());       // o
    console.log((0, o.f)());    // undefined
    console.log((o.f = o.f)()); // undefined
  9. 9

    What is this inside an event handler?

    medium

    In a normal function handler added with addEventListener or an on property, this is the element the listener is attached to, the same as event.currentTarget. In an arrow function handler it is the enclosing this, so use event.currentTarget instead. In React class components, handlers lose this unless bound or defined as arrow class fields. See React interview questions.

  10. 10

    How do you lose this in a class method, and how do you fix it?

    medium

    Class methods are not bound to the instance. Passing instance.method as a callback detaches it, and because class bodies are strict, this becomes undefined and accessing a property throws. Fix it by binding in the constructor, passing an arrow wrapper, or defining the method as an arrow function class field, which creates a bound copy per instance.

    javascript
    class Counter {
      n = 0;
      inc() { return ++this.n; }
      incBound = () => ++this.n;
    }
    const c = new Counter();
    const f = c.inc, g = c.incBound;
    console.log(g());     // 1
    try { f(); } catch (e) { console.log(e.constructor.name); }   // TypeError
  11. 11

    Implement Function.prototype.bind.

    hard

    Return a function that calls the original with the fixed this and the combined arguments. A complete version also handles new, by checking whether the bound function was called as a constructor and then ignoring the bound this. A simple version is acceptable in most interviews if you mention that limitation.

    javascript
    Function.prototype.myBind = function (ctx, ...pre) {
      const fn = this;
      return function bound(...args) {
        return fn.apply(this instanceof bound ? this : ctx, [...pre, ...args]);
      };
    };
    function add(a, b) { return this.base + a + b; }
    console.log(add.myBind({ base: 10 }, 1)(2));   // 13
  12. 12

    Implement call using only apply-free techniques.

    hard

    Temporarily attach the function to the context object under a unique Symbol key, invoke it as a method so implicit binding sets this, then remove the key. Wrap primitives with Object, and default to globalThis when the context is null or undefined.

    javascript
    Function.prototype.myCall = function (ctx, ...args) {
      ctx = ctx == null ? globalThis : Object(ctx);
      const key = Symbol("fn");
      ctx[key] = this;
      const out = ctx[key](...args);
      delete ctx[key];
      return out;
    };
    function hi(x) { return this.n + x; }
    console.log(hi.myCall({ n: "a" }, "b"));   // ab
  13. 13

    What is this at the top level, in a plain function, and in a getter?

    medium

    At the top of a browser script it is window and in an ES module undefined. In Node CommonJS files it is module.exports, which starts as an empty object. In a plain function it is the global object or undefined under strict mode. In a getter or setter it is the object the property is accessed on, which may be an object inheriting from the one that defines the accessor.

  14. 14

    How does this behave with nested functions inside a method?

    medium

    Each non-arrow function gets its own this. A regular function nested in a method called without an object has default binding, even though the outer method has the right this. This is the same bug as the callback case and the same fixes apply.

    javascript
    const o = {
      n: "o",
      run() {
        function inner() { return this === undefined || this === globalThis; }
        const arrow = () => this.n;
        return [inner(), arrow()];
      },
    };
    console.log(o.run());    // [ true, 'o' ]

Frequently asked questions

Why does this become undefined in a function?

Under strict mode, a plain function call has no object, so default binding gives undefined instead of the global object. ES modules and classes are always strict.

Can I change this of an arrow function?

No. call, apply and bind cannot change it. The arrow always uses the this of the scope where it was created.

Which has higher priority, new or bind?

new. When a bound function is called with new, the bound this is ignored and a fresh object is created.

Does this refer to the function itself?

No. It refers to the calling context, not the function object. Use the function name to refer to the function.

Related guides

More in Programming Language Interview Questions by Language

See all in this section