Monday, 2 Jun 2025
  • My Feed
  • My Interests
  • My Saves
  • History
  • Blog
Subscribe
Code Reveals
  • Home
  • HTML

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

    By admin

    What is a Meta Tag in HTML?

    By admin

    What are the different types of HTML tags?

    By admin

    What is Doctype HTML in HTML?

    By admin

    Difference between HTML Tag and HTML Element in HTML?

    By admin
    What is Symentic HTML

    What is Symentic HTML?

    By admin
  • JavaScript

    Explain var let and const in JavaScript with Example.

    By admin

    What is the Event Loop in JavaScript?

    By admin

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

    By admin

    What is the this Keyword in JavaScript?

    By admin

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

    By admin

    How to Reverse a String in JavaScript: Two Essential Methods

    By admin
  • Frontend Interview

    Is JavaScript a synchronous or asynchronous language?

    By admin

    How to Reverse a String in JavaScript: Two Essential Methods

    By admin

    What is Block level Element and Inline Level Element?

    By admin

    Explain Deep Copy and Shallow Copy in JavaScript.

    By admin

    What is Symentic HTML?

    By admin

    What are the Lexical Scope in JavaScript?

    By admin
  • Backend Interview

    What is Arrow and Normal Function in JavaScript?

    By admin

    What is a Closure in JavaScript?

    By admin

    What is a Meta Tag in HTML?

    By admin

    Explain Call, Apply and Bind in JavaScript.

    By admin

    Difference between HTML Tag and HTML Element in HTML?

    By admin

    Mastering Star Rating Systems: Best Practices and Optimized Code Examples

    By admin
  • Other
    • Contact Us
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Submit a Tip
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Code RevealsCode Reveals
  • 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 Blog What is the this Keyword in JavaScript?
JavaScriptJavaScript Interview

What is the this Keyword in JavaScript?

admin
Last updated: February 16, 2025 6:25 pm
admin
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 are the async and defer attributes in the “script” tag?

What are the async and defer Attributes in the <script> Tag? When loading JavaScript in…

By admin

What is Doctype HTML in HTML?

The <!DOCTYPE html> declaration is used to define the document type and version of HTML…

By admin

How can you delay the dispatch in React?

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

By admin

You Might Also Like

JavaScriptJavaScript Interview

What are the Rest and Spread operators in JavaScript?

By admin
Frontend InterviewJavaScriptJavaScript Interview

Is JavaScript a synchronous or asynchronous language?

By admin
JavaScriptJavaScript Interview

What is one-way data binding in React?

By admin
What are the Lexical Scope in JavaScript
Frontend InterviewJavaScript

What are the Lexical Scope in JavaScript?

By admin

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
  • Submit a Tip

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?