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.
| Level | Fresher to 4 years |
|---|---|
| Focus | Binding rules, call/apply/bind, arrow functions |
| Runtime | Browser 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.
- Default binding gives the global object in sloppy mode and undefined in strict mode
- bind creates a new function whose this cannot be changed again
- Class bodies and modules are always strict
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 What is this in JavaScript?
easythis 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 Predict the output: a method detached from its object.
mediumo.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.
javascriptconst 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 callback3 How do call, apply and bind differ?
mediumAll 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.
javascriptfunction 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 Predict the output: what happens when you bind twice, or call a bound function with call?
hardA 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.
javascriptfunction 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" })); // first5 How do arrow functions treat this?
mediumAn 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.
javascriptconst 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 Predict the output: a callback inside a method.
hardA 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.
javascriptconst 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 What does this refer to inside a constructor, and what does new do?
mediumnew 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.
javascriptfunction 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 Predict the output: the comma operator and parenthesised method calls.
hardA 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.
javascriptconst 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)()); // undefined9 What is this inside an event handler?
mediumIn 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 How do you lose this in a class method, and how do you fix it?
mediumClass 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.
javascriptclass 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); } // TypeError11 Implement Function.prototype.bind.
hardReturn 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.
javascriptFunction.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)); // 1312 Implement call using only apply-free techniques.
hardTemporarily 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.
javascriptFunction.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")); // ab13 What is this at the top level, in a plain function, and in a getter?
mediumAt 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 How does this behave with nested functions inside a method?
mediumEach 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.
javascriptconst 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
- JVM Internals Interview Questions
- MySQL Interview Questions and Answers
- Nth Highest Salary in SQL: Interview Questions
- PL/SQL Interview Questions and Answers
- PostgreSQL Interview Questions and Answers
- Python Coding Interview Questions with Solutions
- Python Decorators Interview Questions
- Python Exception Handling Interview Questions
- Python File Handling Interview Questions
- Python Generators and Iterators Interview Questions
- Python Interview Questions and Answers
- Python Lambda, Map, Filter and Reduce Interview Questions