logologo

Three-D

☕Sign in

    Recent Post:

    Categories:

    nextjsjavascriptthreejshonoreactjs
    featured post image

    snippets

    featured post image

    What is Currying?

    featured post image

    What is an IIFE?

    Understanding the this Keyword in JavaScript

    September 22, 2024

    595

    image

    Think you know it all?

    Take quiz!

    In JavaScript, the this keyword is a special identifier that refers to the context in which a function is called. Its value can change depending on how a function is invoked. Understanding this is crucial for writing clear and effective JavaScript code.

    1. The Global Context

    In the global context (outside of any function), this refers to the global object. In browsers, this is the window object.

    code

    console.log(this); // In a browser, this will log the window object

    2. Inside a Function

    When used inside a regular function, this refers to the global object (in non-strict mode). In strict mode, it will be undefined.

    code

    function showThis() {
        console.log(this);
    }
    
    showThis(); // Output: Window object (non-strict mode)

    code

    "use strict";
    
    function showThis() {
        console.log(this);
    }
    
    showThis(); // Output: undefined

    3. Inside an Object Method

    When a function is called as a method of an object, this refers to that object.

    code

    const person = {
        name: "Satyendra",
        greet: function() {
            console.log("Hello, " + this.name);
        }
    };
    
    person.greet(); // Output: Hello, Satyendra

    Here, this inside the greet method refers to the person object, allowing access to its properties.

    4. Constructor Functions

    When a function is used as a constructor (invoked with the new keyword), this refers to the newly created object.

    code

    function Person(name) {
        this.name = name;
    }
    
    const person1 = new Person("Satyendra");
    console.log(person1.name); // Output: Satyendra

    5. Arrow Functions

    Arrow functions do not have their own this. Instead, they inherit this from the surrounding lexical context.

    code

    const person = {
        name: "Satyendra",
        greet: function() {
            const innerFunction = () => {
                console.log("Hello, " + this.name);
            };
            innerFunction();
        }
    };
    
    person.greet(); // Output: Hello, Satyendra

    The innerFunction is an arrow function, so it inherits this from the greet method, which is the person object.

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

    You can explicitly set the value of this using call(), apply(), or bind().

    code

    function greet() {
        console.log("Hello, " + this.name);
    }
    
    const person1 = { name: "Satyendra" };
    const person2 = { name: "John" };
    
    greet.call(person1); // Output: Hello, Satyendra
    greet.call(person2); // Output: Hello, John

    code

    const person = { name: "Satyendra" };
    const greet = function() {
        console.log("Hello, " + this.name);
    }.bind(person);
    
    greet(); // Output: Hello, Satyendra

    Summary

    The this keyword refers to the context in which a function is called, not where it is defined.

    Its value varies depending on the invocation context: global, object method, constructor function, or arrow function.

    You can control this using call(), apply(), and bind().