Skip to content

this keyword in javascript

Posted on:December 5, 2023 at 02:05 AM

Table of contents

Open Table of contents

“this” keyword simple example

Let’s take a simple example to understand how this keyword works in javascript.

console.log(this);

What do you think would be output of the above code?

Now let’s add one line to this code.

"use strict";
console.log(this);

This keyword inside normal function

function test() {
  console.log(this);
}
test();

function test1() {
  "use strict";
  console.log(this);
}
test1();

when we use function call, remember this would be either undefined or window/node object

This keyword inside object

const test = {
  name: "Prem",
  getName: function () {
    console.log(this);
  },
};

test.getName();

Creating functions inside objects are called methods. And whenever we call object.methodName(), this value will be object itself.

Examples

const test = {
  name: "Prem",
  getName: function () {
    console.log(this); // what will be output of this keyword
    function myName() {
      console.log(this); // what will be output of this keyword
    }
    myName();
  },
};
test.getName();

can you guess what will be the output of this keyword in above code?

always remember, it doesn’t matter where this keyword is placed. How function are called is matter.

call, apply and bind and how these methods changes the values of this keyword.

call and apply

Let’s take the above example only. if you take a closure look into myName function. this value inside that function is either node/window or undefined. But what if we want to get name value when we use this.name. This is where we can use call and apply methods.

const test = {
  name: "Prem",
  getName: function () {
    console.log(this.name); // "Prem"
    function myName() {
      console.log(this.name); // "Prem"
    }
    myName.call(this); // myName.apply(this)
  },
};
test.getName();

whenever we call any function using call, apply and bind, two things happens

Let’s take an example and understand.

const test = {
  name: "Prem",
  getName: function () {
    return this.name;
  },
};

const test1 = {
  name: "Divye",
};

test1 object doesn’t have any methods on it. But what if we want to use getName method to get test1’s value ?

const res1 = test.getName(); // "Prem"
const res2 = test.getName.call(test1); // "Divye"
const res2 = test.getName.apply(test1); // "Divye"

There is only one difference between call and apply

Bind

bind works exactly similar to how call and apply works. you can use call and apply whenever you want to get result by directly calling. But Bind returns function and after calling that function you get the value.

const res = test.getName.bind(test1);
res(); // "Divye"

Arrow function and this keyword

if we have a this keyword inside arrow function, this keyword takes value from outer scope of the function. Now let’s take an example

function test() {
  // this -> whatever this value be here, it will be inside the arrow function
  const arrowFunc = () => {
    console.log(this); // either undefined or window/Node object
  };
  arrowFunc();
}
test();

const test1 = {
  name: "Prem",
  getName: function () {
    console.log(this.name); // "Prem"
    const myName = () => {
      console.log(this.name); // if this.name is Prem in parent scope, it will be similar inside arrow function
    };
    myName();
  },
};
test1.getName();

class and this keyword

Let’s understand this keyword value when we use class in javascript.

class Animal {
  constructor(name) {
    this.name = name;
  }

  getName() {
    return this.name;
  }
}

const animal = new Animal("Dog");
console.log(animal);

When we use new keyword to create new object this becomes instance of that object.