Skip to main content

this in JavaScript: The Actor Who Changes Roles

Updated 8 min readMarkdown
this in JavaScript: The Actor Who Changes Roles

What is this in JavaScript ?

In almost all other languages, conditionals, loops, functions and these basic things are almost the same. But in JavaScript, this is a keyword that makes JavaScript different.

this also exists in other languages, but in JavaScript it is used at a very high level. And after learning this , we can easily understand call, apply, bind and new later.


this in Real World:

Just suppose this is an actor. The same actor plays different roles in different movies. Actor is same, but the role depends on the movie.

In the same way, the value of this is not fixed. It depends on where and how the code is run.

Or understand it like this: every person behaves different in his own area. At home different, in office different. this also changes according to its environment.


this in the Global Context

Global context means: no function, no object, just this is written.

console.log(this);
  • In Browser console: we get the Window object

  • In Node.js: we get an empty object {}

Code is same, but environment is different, that`s why this is different.

Small Update for Node.js:

  • The {} result comes when your file is a CommonJS module (the one which uses require).

  • But if you use ES Modules (the one which uses import), then this will be undefined there.

// CommonJS file (require)
console.log(this); // {}

// ES Module file (import)
console.log(this); // undefined

As we can see visually in the diagram below:


this Inside Objects

Inside an object, this points to that same object. It is like the object is saying "my own properties". So with this, we can easily access other properties of the same object from inside its method.

const bollywoodFilm = {
    name: "Bajirao Mastani",
    lead: "Ranveer",
    introduce() {
        return `${this.lead} performs in ${this.name}`;
    },
};

console.log(bollywoodFilm.introduce());
// Output: Ranveer performs in Bajirao Mastani

Here introduce() is called by bollywoodFilm, so this = bollywoodFilm. That is why this.lead gives "Ranveer" and this.name gives "Bajirao Mastani".

Easy way to Remember: Look at the left side of the dot. this is calling the method.

bollywoodFilm.introduce();
// ↑ left of the dot = bollywoodFilm = this

That`s how we can easily use this anywhere inside the object's methods, but remember, it works properly with regular methods. Arrow functions are different.


this Inside Functions

Now let's see this inside a normal function, which is not inside any object.

function showThis() {
    return typeof this;
}

console.log(showThis());
// Output: object

Here nobody is calling the function with a dot, so this becomes the global object.

  • In Browser: Window object

  • In Node.js: Nodejs global object

But using Strict Mode :

If we write "use strict" inside the function, the result will be different.

function showThisStrict() {
    "use strict";
    return typeof this;
}

console.log(showThisStrict());
// Output: undefined

In strict mode, JavaScript does not give the global object. this is just undefined.

Strict mode is safer, because we don't accidentally touch the global object.

Quick Summary

Function type Value of this
Normal function (no strict) Global object
Normal function (strict mode) undefined

In a function, always check who is calling it. If nobody is calling it with a dot, then this is global object (or undefined in strict mode).


How Calling Context Changes this

The most important point: this does not depend on where the function is written. It depends on who is calling it.

javascript

const actor = {
    name: "Ranveer",
    bow() {
        return `${this.name} takes a bow`;
    },
};

console.log(actor.bow());
// Output: Ranveer takes a bow

const detachedBow = actor.bow;
console.log(detachedBow());
// Output: undefined takes a bow

In the first call, actor is calling bow(), so this = actor.

In the second call, we took the method out of the object and called it alone. Now nobody is on the left of the dot, so this is lost. This is called a detached method: the method loses its context.


Can We Set this Ourselves?

In the last example, detachedBow() lost its this. Normally JavaScript decides this by itself, depending on who is calling the function.

But JavaScript also gives us three methods to set this manually: call(), apply() and bind().


call()

call() runs the function immediately, and we decide what this will be. The first value we pass is this.

We can borrow a method from one object and use it for another object. Here we use introduce() from bollywoodFilm for a different film:

const bollywoodFilm = {
    name: "Bajirao Mastani",
    lead: "Ranveer",
    introduce() {
        return `${this.lead} performs in ${this.name}`;
    },
};

const anotherFilm = {
    name: "Pathaan",
    lead: "Shah Rukh",
};

console.log(bollywoodFilm.introduce.call(anotherFilm));
// Output: Shah Rukh performs in Pathaan

Let's understand how this works:

  1. bollywoodFilm.introduce gives us the function. It does not run yet.

  2. .call(anotherFilm) runs that function immediately and sets this = anotherFilm.

  3. Inside the function, this.lead is now "Shah Rukh" and this.name is "Pathaan".

So the method belongs to bollywoodFilm, but anotherFilm borrowed it without writing it again. This is called method borrowing.

If the function needs arguments, we pass them one by one after this:

function announce(city, day) {
    return `${this.name} releases in ${city} on ${day}`;
}

console.log(announce.call(anotherFilm, "Lahore", "Friday"));
// Output: Pathaan releases in Lahore on Friday

apply()

apply() does the same work as call(). The only difference is how we send the arguments: in one array.

console.log(announce.apply(anotherFilm, ["Lahore", "Friday"]));
// Output: Pathaan releases in Lahore on Friday

Use apply() when your arguments are already inside an array.


bind()

bind() does not run the function. It returns a new function in which this is locked.

This is the fix for our detached method problem:

const actor = {
    name: "Ranveer",
    bow() {
        return `${this.name} takes a bow`;
    },
};

const detachedBow = actor.bow;
console.log(detachedBow());
// this is lost here (the output depends on the environment, as we saw above)

const boundBow = actor.bow.bind(actor);

console.log(boundBow());
// Output: Ranveer takes a bow

Let's understand how this works:

  1. actor.bow.bind(actor) does not run the function. It gives us a new function.

  2. In this new function, this is locked to actor.

  3. We store it in boundBow, so now we can call it any time, and it will always say "Ranveer takes a bow".

That is why bind() is useful when a method is passed alone as a callback, for example in a timer or an event handler. Without bind(), the method loses its this, as we saw in detachedBow.


call() vs apply() vs bind()

call() apply() bind()
Runs the function? Yes, immediately Yes, immediately No, returns a new function
Arguments One by one In an array One by one
Returns Result of the function Result of the function A new function
this Set for this call only Set for this call only Locked inside the new function

Easy way to Remember:

  • Call = Comma (arguments separated by commas)

  • Apply = Array (arguments in an array)

  • Bind = Baad mein (runs later, it gives a new function)


What About Arrow Functions?

Arrow functions are different. They do not have their own this. They take this from the place where they are written, so call(), apply() and bind() cannot change it.

That is why we use normal functions when we want this to depend on the caller.


Quick Note:

  • Global context: Window in browser, {} in Node.js (CommonJS)

  • Inside object method: this = the object itself

  • Inside normal function: global object, or undefined in strict mode

  • Calling context: always check who is calling the function

  • call(): runs now, arguments one by one

  • apply(): runs now, arguments in an array

  • bind(): runs later, returns a new function with locked this


If you did not find a topic you were looking for, please check my notes. I hope you will find it there: FullStackHub / Learn JavaScript

You can find more of my work at abdulrdeveloper.me

Read more posts at blog.abdulrdeveloper.me

About the author

  • CS student and full-stack developer from Pakistan. Building with JavaScript, TypeScript, React, Next.js and Node.js. Currently in ChaiCode Web Dev Cohort 2026.

No comments yet. Be the first to comment.

More from this blog