Last Updated : 04 Jan, 2025
In JavaScript, this is a special keyword that refers to the current object or context. It behaves differently depending on where it's used. If you use this in a regular function, it refers to the global object (in browsers, this is the window). In an object's method, this refers to the object itself.
We are going to discuss some Interview Questions related to This keyword.
1. Why this is Necessary in JavaScript?
console.log(this); // Window (browser) or global object (Node.js)
3. How does this behave inside a simple function? JavaScript
function show() {
console.log(this); // Global object or undefined in strict mode
}
show();
<ref *1> Object [global] { global: [Circular *1], clearInterval: [Function: clearInterval], clearTimeout: [Function: clearTimeout], setInterval: [Function: setInterval], setTimeout: [Functio...4. What is the value of this in strict mode? JavaScript
"use strict";
function test() {
console.log(this);
}
test();
5. How does 'this' work in an object method? JavaScript
const obj = {
name: "GeeksforGeeks",
greet() {
console.log(this.name);
}
};
obj.greet();
6. How does 'this' work with nested functions? JavaScript
const obj = {
name: "GeeksforGeeks",
greet() {
function inner() {
console.log(this);
}
inner();
}
};
obj.greet();
<ref *1> Object [global] { global: [Circular *1], clearInterval: [Function: clearInterval], clearTimeout: [Function: clearTimeout], setInterval: [Function: setInterval], setTimeout: [Functio...
const obj = {
name: "Geeks",
greet: () => {
console.log(this.name); // undefined, as `this` refers to the enclosing scope
}
};
obj.greet();
8. What is the value of this inside a class? JavaScript
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(this.name); // Refers to the instance of the class
}
}
const p = new Person("GeeksforGeeks");
p.greet(); // "GeeksforGeeks"
9. What happens to 'this' in an IIFE? JavaScript
(function () {
console.log(this); // Global object or undefined in strict mode
})();
<ref *1> Object [global] { global: [Circular *1], clearInterval: [Function: clearInterval], clearTimeout: [Function: clearTimeout], setInterval: [Function: setInterval], setTimeout: [Functio...
The output of the code depends on whether you are running it in strict mode or non-strict mode.
Non-strict mode (default in regular JavaScript): In non-strict mode, when a function is called without an object context (like in your case), this refers to the global object (in the browser, it’s window).
JavaScript
// In a browser:
Window { ... } // Represents the global window object in a browser
Strict mode: In strict mode ('use strict';), this inside a function call refers to undefined instead of the global object.
10. How does 'this' behave in setTimeout? JavaScript
const obj = {
name: "GeeksforGeeks",
greet() {
setTimeout(function () {
console.log(this.name); // undefined in strict mode
}, 1000);
}
};
obj.greet();
11. How to fix 'this' in setTimeout using arrow functions? JavaScript
const obj = {
name: "GeeksforGeeks",
greet() {
setTimeout(() => {
console.log(this.name); // "GeeksforGeeks"
}, 1000);
}
};
obj.greet();
In this code:
const obj = { name: "GeeksforGeeks" };
function greet() {
console.log(this.name);
}
const boundGreet = greet.bind(obj);
boundGreet();
In this code:
const obj = { name: "GeeksforGeeks" };
function greet() {
console.log(this.name);
}
greet.call(obj);
greet.apply(obj);
GeeksforGeeks GeeksforGeeks
In this code:
GeeksforGeeks GeeksforGeeks14. What happens when "this" is used in a constructor function? JavaScript
function Person(name) {
this.name = name;
}
const p = new Person("GeeksforGeeks");
console.log(p.name);
In this code
const obj = {
count: 0,
increment() {
this.count++;
return this;
},
show() {
console.log(this.count);
return this;
}
};
obj.increment().increment().show();
In this code
In an ES module, this is undefined at the top level because ES modules run in strict mode by default
JavaScript
console.log(this); // Undefined in Es modules
17. What is " this " inside the an object returned by the function? JavaScript
function createObject() {
return {
name: "GeeksforGeeks",
greet() {
console.log(this.name);
}
};
}
const obj = createObject();
obj.greet();
In this example, the createObject function returns an object with a name property and a greet method. When obj.greet() is called, this refers to the obj itself, so it accesses obj.name and prints "GeeksforGeeks".
18 . How does "this" behave in a factory function when used inside a method of the returned object? JavaScript
function create(name) {
return {
name,
greet() {
console.log(this.name);
}
};
}
const person = create("GeeksforGeeks");
person.greet();
In this code
[1, 2, 3].forEach(function () {
console.log(this);
}, { name: "GeeksforGeeks" });
{ name: 'GeeksforGeeks' } { name: 'GeeksforGeeks' } { name: 'GeeksforGeeks' }
In this code
const obj = {
name: "GeeksforGeeks",
greet() {
return Promise.resolve(this.name).then(name => {
console.log(name);
});
}
};
obj.greet();
const obj = {
name: "Geeks",
get greet() {
return `Hello, ${this.name}`;
},
set greet(newName) {
this.name = newName;
}
};
console.log(obj.greet); // Output: Hello, Geeks
obj.greet = "forGeeks";
console.log(obj.greet); // Output: Hello, forGeeks
Hello, Geeks Hello, forGeeks
In this example, obj has a getter and setter for the greet property. The getter returns a greeting message with this.name, and the setter updates this.name. Initially, obj.greet prints "Hello, Geeks". After setting obj.greet = "forGeeks", it updates name and prints "Hello, forGeeks".
21. How does " this " behave with async iteration? JavaScript
const obj = {
name: "GeeksforGeeks",
async *greet() {
yield `Hello, ${this.name}`;
}
};
(async () => {
for await (const message of obj.greet()) {
console.log(message);
}
})();
Hello, GeeksforGeeks
In this code
const map = new Map();
map.set("key1", { name: "Geeks" });
map.set("key2", { name: "forGeeks" });
map.forEach(function (value, key) {
console.log(this.name);
}, { name: "GeeksforGeeks" });
GeeksforGeeks GeeksforGeeks
GeeksforGeeks GeeksforGeeks23 .How does "this" behave with async/await in JavaScript? JavaScript
const obj = {
name: "GeeksforGeeks",
async greet() {
console.log(this.name);
}
};
obj.greet();
In this example, obj.greet() is an asynchronous function that logs this.name. Since this refers to the obj object, it prints "GeeksforGeeks", which is the value of obj.name.
24. What is the value of " this " in an event handler in a DOM element? JavaScript
const obj = {
name : "GeeksforGeeks",
greet() {
document.getElementById("btn").addEventListener(
"click", function() {
console.log(this.name); // Output: undefined
});
}
};
obj.greet();
25. How does " this " work with new and constructor functions in JavaScript?
function Animal(name)
{
this.name = name;
this.speak = function() {
console.log(`${this.name} makes a sound`);
};
}
const dog = new Animal("Dog");
dog.speak();
In this code
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4