Easy to understand 5 rules, that cover the behaviour of the "this" keyword in different contexts and helps you to master this keyword for any javascript interview.
Anuj Sharma
Last Updated Feb 22, 2025
Advertisement
"This" keyword is a very important concept to understand for any JavaScript Interview. 'this' keyword behaves differently in different contexts, so it is important to understand those contexts & how to know "this" value in each context.
Here are 5 examples that cover all different behaviour of the "this" keyword in JavaScript:
In the global context (outside of any function), this refers to the global object, which is the window object in a browser environment. Only var
can be defined as part of the window (global object), let and const not attached to the global object (window) and remains undefined
💡 In 'use strict' mode this doesn't map to window object and remains undefined.
var name = 'Random';
let car = 'BMW';
const city = 'jaipur';
console.log(this === window); // true
console.log(window.name); // Random
console.log(this.name, this.car, this.city);
// Random undefined undefined
console.log(this.name, car, city);
// Random BMW jaipur
Inside a function, the value of this depends on how the function is called. If the function is called as a method of an object, this refers to the object (case 1). In case function is called in global object, this refers to the global object (case 2)
let obj = {
name: 'Alice',
greet: function() {
console.log(`Hello, my name is ${this.name}`);
}
};
// Case 1: How function called - with object context
obj.greet(); // "Hello, my name is Alice"
// Case 2: How function called - with window context, like window.globalFunc
const globalFunc = obj.greet;
/*
globalFunc invokes greet() in global context and as described in step 1,
"this" maps to windows in global context,
Code tries to find the name in global context, but doesn't find so return undefined.
*/
globalFunc(); // "Hello, my name is"
When a function is used as a constructor (i.e., called with the new keyword), this refers to the newly created object.
function Person(name) {
this.name = name;
}
let alice = new Person('Alice');
console.log(alice.name); // "Alice"
The value of this can be explicitly set using the call, apply, or bind methods.
function greet() {
console.log(`Hello, my name is ${this.name}.`);
}
let alice = {name: 'Alice'};
greet.call(alice); // "Hello, my name is Alice."
Arrow functions do not have their own "this" value. Instead, they inherit this from the enclosing lexical scope means parent lexical scope.
let obj = {
name: 'Alice',
greet: () => {
console.log('Hello, my name is', this.name);
}
};
obj.greet(); // "Hello, my name is undefined."
Advertisement
Advertisement
Advertisement
Alok Kumar Giri
Last Updated Jun 2, 2025
Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.
Anuj Sharma
Last Updated Aug 3, 2025
Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.
Anuj Sharma
Last Updated Jun 13, 2025
A comprehensive cheat sheet for the Frontend Machine Coding Interview Round, which helps to revise all the important machine coding & UI design concepts before the interview.
Anuj Sharma
Last Updated Jun 27, 2025
A detailed list of 20+ most asked Frontend Machine Coding Interview Questions and resources both in JavaScript & React. Also covers expected functional/Non-functional requirements in this Interview.
Vijay Sai Krishna vsuri
Last Updated Aug 21, 2025
A Quick guide about popstate event in JavaScript, If you’ve ever hit the back button in your browser and wondered how your Single-Page Application knows which view to render, this guide is for you.
Last Updated Aug 21, 2025
A beginner-friendly guide to understanding call, apply, and bind methods in JavaScript, along with step-by-step call, apply and bind polyfill implementations that are often asked in interviews.
© 2024 FrontendGeek. All rights reserved