šŸš€

Build an AI SaaS in Days, Not Months

Buy Now a production-ready AI SaaS starter with login, payments, DB, and AI Integrations built in.

šŸ“¢

Advertise Your Company Here

Click now to email us to advertise in this spot and reach thousands of frontend developers.

šŸš€

Build an AI SaaS in Days, Not Months

Buy Now a production-ready AI SaaS starter with login, payments, DB, and AI Integrations built in.

šŸ“¢

Advertise Your Company Here

Click now to email us to advertise in this spot and reach thousands of frontend developers.

Blog/NotesConcept

5 Ultimate Rules to master this keyword in JavaScript

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.

Beginner

Anuj Sharma

Last Updated Nov 11, 2025


"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.

5 Ultimate Rules to master 'this' keyword

Here are 5 examples that cover all different behaviour of the this keyword in JavaScript:

Rule 1: Global Context

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

Rule 2: Function Context

Inside a function, the value of this depends on how the function is called. Let's go through the 2 cases to understand this rule.

Case 1: If the function is called as a method of an object, this refers to the object.

Case 2: In case function is called in global object, this refers to the global object.

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"

Rule 3: Constructor Context

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"

Rule 4: Explicit Binding using call, apply or bind

The value of this can be explicitly set using the callapply, 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."

Rule 5: Arrow Functions

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."

Further Reading

  1. Best resources to prepare for JavaScript Interview
  2. JavaScript Essentials: Call, Apply and Bind Methods Explained with Polyfills
  3. 20+ Frontend Machine Coding Round Interview Questions

šŸš€

Love this content? Share it!

Help others discover this resource

Comments

Be the first to share your thoughts!

Guest User

Please login to comment

0 characters


No comments yet.

Start the conversation!

Share Your Expertise & Help the Community!

Build Your Portfolio

Help the Community

Strengthen Your Skills

Share your knowledge by writing a blog or quick notes. Your contribution can help thousands of frontend developers ace their interviews and grow their careers! šŸš€


Other Related Blogs

Implement useFetch() Custom Hook in React (Interview)

Anuj Sharma

Last Updated Nov 23, 2025

Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.

Flatten Nested Array in JavaScript using Recursion

Anuj Sharma

Last Updated Nov 24, 2025

Understand step by step how to flatten nested array in javascript using recursion, also explore the flatten of complex array of object.

Polyfill for map, filter, and reduce in JavaScript

Anuj Sharma

Last Updated Oct 2, 2025

Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.

Master Hoisting in JavaScript with 5 Examples

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.

setTimeout Polyfill in JavaScript - Detailed Explanation

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.

21 Most Asked Frontend System Design Interview Questions & Patterns

Anuj Sharma

Last Updated Dec 17, 2025

A comprehensive collection of the most asked Frontend System Design Interview Questions for Experienced along with the related Answers, Patterns and Important Resources.

Stay Updated

Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.

FrontendGeek
FrontendGeek

All in One Preparation Hub to Ace Frontend Interviews. Master JavaScript, React, System Design, and more with curated resources.

Product

HomeFrontend InterviewInterview ExperienceBlogsToolsLeaderboard

Tools

CSS Image FilterPixelate ImageAspect Ratio CalculatorBox Shadow GeneratorCSS Gradient GeneratorNeumorphism GeneratorExplore More Tools →

Ā© 2026 FrontendGeek. All rights reserved