Monday, 4 Aug 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
Focus - Code Reveals
  • Home
  • HTML

    Difference between HTML Tag and HTML Element in HTML?

    By Chief Editor

    What is the difference between “HTML” and “HTML5”?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    What are the different types of HTML tags?

    By Chief Editor
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor
  • JavaScript

    What are the Rest and Spread operators in JavaScript?

    By Chief Editor

    What is a Closure in JavaScript?

    By Chief Editor

    What is Scope in JavaScript?

    By Chief Editor
    What are the Lexical Scope in JavaScript

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    Explain Call, Apply and Bind in JavaScript.

    By Chief Editor
  • Frontend Interview

    What is Position in CSS?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor
  • Backend Interview

    What is Virtual DOM in React?

    By Chief Editor

    What is a Function Component in React?

    By Chief Editor

    What is Synthetic Event?

    By Chief Editor

    What is Redux, and how does it work?

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    What is one-way data binding in React?

    By Chief Editor
  • Nodejs
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Focus - Code RevealsFocus - Code Reveals
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • HTML
  • CSS
  • Backend Interview
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.
Home Blog What is the this Keyword in JavaScript?
JavaScriptJavaScript Interview

What is the this Keyword in JavaScript?

Chief Editor
Last updated: February 16, 2025 6:25 pm
Chief Editor
Share
SHARE

The this keyword in JavaScript refers to the object that is executing the current function. Its value depends on how and where the function is called.

Contents
Key Rules for this in Different Contexts:Summary Table:

Key Rules for this in Different Contexts:

1. Global Context:

In the global scope or outside any function:

  • In browser environments, this refers to the window object.
  • In strict mode ('use strict'), this is undefined.
javascriptCopyEditconsole.log(this); // Window (in browsers)

2. Inside a Function (Non-Strict Mode):

  • If a function is called in the global context, this refers to the window object.
javascriptCopyEditfunction show() {
  console.log(this);
}
show(); // Window
  • In strict mode:
javascriptCopyEdit'use strict';
function show() {
  console.log(this);
}
show(); // undefined

3. Inside an Object Method:

  • When a function is called as a method of an object, this refers to the object.
javascriptCopyEditconst user = {
  name: 'John',
  greet: function() {
    console.log(this.name); // Refers to `user` object
  }
};
user.greet(); // 'John'

4. Inside an Arrow Function:

  • Arrow functions do not have their own this.
  • this is lexically inherited from the surrounding scope.
javascriptCopyEditconst user = {
  name: 'John',
  greet: () => {
    console.log(this.name); // `this` refers to the outer scope (e.g., `window` in global)
  }
};
user.greet(); // undefined (or `window.name` in browsers)

Another example (lexical this):

javascriptCopyEditconst user = {
  name: 'John',
  greet: function() {
    const arrowFunc = () => {
      console.log(this.name);
    };
    arrowFunc();
  }
};
user.greet(); // 'John' because arrow function inherits `this` from `greet()`

5. In Event Listeners:

  • In a DOM event listener, this refers to the HTML element that received the event.
javascriptCopyEditdocument.querySelector('button').addEventListener('click', function() {
  console.log(this); // The button element
});

6. With call(), apply(), and bind():

These methods allow you to manually set this.

  • call() – Immediately invokes a function with a specified this value.
  • apply() – Same as call(), but arguments are passed as an array.
  • bind() – Returns a new function with this permanently set.
javascriptCopyEditfunction show() {
  console.log(this.name);
}

const user = { name: 'John' };

show.call(user); // 'John'
show.apply(user); // 'John'

const boundShow = show.bind(user);
boundShow(); // 'John'

Summary Table:

Contextthis Value
Global Scopewindow (or global in Node.js), undefined in strict mode
Function (non-strict)window
Function (strict mode)undefined
Object MethodThe object that the method is called on
Arrow FunctionInherits this from surrounding scope
Event ListenerThe element that received the event
call(), apply(), bind()Set explicitly by the method

Understanding this is fundamental to mastering JavaScript, especially when working with objects, classes, and event handling.

Share This Article
Email Copy Link Print
Previous Article What is Scope in JavaScript?
Next Article What is Arrow and Normal Function in JavaScript?
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Your Trusted Source for Accurate and Timely Updates!

Our commitment to accuracy, impartiality, and delivering breaking news as it happens has earned us the trust of a vast audience. Stay ahead with real-time updates on the latest events, trends.
FacebookLike
XFollow
InstagramFollow
YoutubeSubscribe
LinkedInFollow
QuoraFollow
- Advertisement -
Ad imageAd image

Popular Posts

How can you delay the dispatch in React?

To delay a dispatch in React, especially when using something like Redux or React state…

By Chief Editor

System Design and Frontend System Design: An In-depth Overview

Introduction System design is an important component of software engineering that entails the system architecture,…

By Chief Editor

What is Callback Hell in JavaScript?

Callback Hell in JavaScript refers to a situation where you have multiple nested callbacks, making…

By Chief Editor

You Might Also Like

JavaScriptJavaScript Interview

What is Arrow and Normal Function in JavaScript?

By Chief Editor
Frontend InterviewJavaScript

Explain Deep Copy and Shallow Copy in JavaScript.

By Chief Editor
JavaScriptJavaScript Interview

What are the Rest and Spread operators in JavaScript?

By Chief Editor
JavaScript

What is hoisting in JavaScript with an example?

By Chief Editor

Focus by CodeReveals is your dedicated platform for mastering tech interviews and advancing your development skills. Whether you’re aiming to become a Frontend Developer, Backend Developer, or simply preparing for your next big interview, we’re here to guide you every step of the way.

Our mission is to help aspiring developers gain real-world knowledge, build confidence, and succeed in technical interviews. We offer structured content, curated interview questions, coding challenges, and practical guidance — all designed by experienced professionals who understand what top tech companies are looking for.

At Improve, we don’t just teach — we prepare you to think like a developer, solve like an engineer, and present like a pro.

Join us and start improving today — because your dream tech job is within reach

Most Famous
  • HTML
  • CSS
  • JavaScript
  • NodeJS
Top Categories
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
Usefull Links
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?