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

    What is Symentic HTML?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor

    Difference between HTML Tag and HTML Element 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
  • JavaScript

    What is Arrow and Normal Function in JavaScript?

    By Chief Editor

    What is a Closure in JavaScript?

    By Chief Editor

    Explain var let and const in JavaScript with Example.

    By Chief Editor

    What is Scope in JavaScript?

    By Chief Editor

    What are the Rest and Spread operators in JavaScript?

    By Chief Editor

    Explain Call, Apply and Bind in JavaScript.

    By Chief Editor
  • Frontend Interview

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor
  • Backend Interview

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What is Scope in JavaScript?

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    Explain the useState and useEffect hooks in React

    By Chief Editor

    What is a Closure in JavaScript?

    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

What is React Fiber and its importance in react?

React Fiber is the reimplementation of React's core reconciliation algorithm, introduced in React 16 (2017).It…

By Chief Editor

What is a Function Component in React?

A Function Component in React is a JavaScript function that returns React elements (JSX) representing…

By Chief Editor

What is Arrow and Normal Function in JavaScript?

Arrow Function vs Normal Function in JavaScript In JavaScript, Arrow Functions and Normal Functions (also…

By Chief Editor

You Might Also Like

JavaScriptJavaScript Interview

What is a Promise in JavaScript, and what are its parameters?

By Chief Editor
JavaScriptJavaScript Interview

What is Scope in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

What are the Rest and Spread operators in JavaScript?

By Chief Editor
Frontend InterviewJavaScript

Explain Deep Copy and Shallow Copy in JavaScript.

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?