Friday, 18 Jul 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
improve-logo improve-logo
  • 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 are the different types of HTML tags?

    By Chief Editor

    What are the async and defer attributes in the “script” tag?

    By Chief Editor
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor
  • JavaScript

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    What are the map, filter, and reduce methods in JavaScript?

    By Chief Editor

    Explain var let and const in JavaScript with Example.

    By Chief Editor

    Difference between document.createElement and document.createElementFragement 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

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What is Position in CSS?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor
  • Backend Interview

    What is one-way data binding in React?

    By Chief Editor

    What are controlled and uncontrolled components in React?

    By Chief Editor

    Explain var let and const in JavaScript with Example.

    By Chief Editor

    What are the Rest and Spread operators in JavaScript?

    By Chief Editor

    How can you delay the dispatch in React?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor
  • Nodejs
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
ImproveImprove
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • Pages
    • Home
    • Blog Index
    • Contact Us
    • Search Page
    • 404 Page
  • Features
    • Post Headers
    • Layout
  • Personalized
    • My Feed
    • My Saves
    • My Interests
    • History
  • About
  • Categories
    • Technology
  • Categories
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.

Home 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

What is Flex Box in CSS?

Flexbox, short for Flexible Box, is a CSS layout model designed to make it easier…

By Chief Editor

What is Symentic HTML?

Semantic HTML refers to using HTML tags that convey the meaning or purpose of the…

By Chief Editor

You Might Also Like

JavaScript

What is a Closure in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

What is Arrow and Normal Function in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

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

By Chief Editor
JavaScriptJavaScript Interview

What is memoization in JavaScript?

By Chief Editor
improve-logo

Code Reveals is a cutting-edge software development company dedicated to delivering high-quality, scalable, and innovative solutions for businesses of all sizes. Our team of expert developers, designers, and engineers specializes in creating custom software, web applications, mobile apps, and enterprise solutions that are tailored to meet the unique needs of our clients.

Most Famous
  • HTML
  • CSS
  • JavaScript
  • Node
Top Categories
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
Usefull Links
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?