Stop 07 · Modern JavaScriptTopics · 4/9

The this Keyword

Part of the Web Development Roadmap

In short

this is JavaScript's way of saying 'whoever called me'. It changes depending on how a function is called, which is why it confuses almost everyone at first.

this refers to the object a function is called on, and its value depends on how the function is called. Arrow functions don't have their own this, which often fixes confusing bugs.

The jaadu mascot and a friend each point at themselves while holding similar phones.

Think of it like this

The word 'mine'

When Priya says "this phone is mine", it's Priya's phone. When Rahul says the exact same sentence, it's Rahul's. The words don't change, the speaker does. With this, the caller decides the meaning, not the place the function was written.

Watch it happen

Who called hello()?

student
hello()
lost

student.hello()

1/5student is left of the dot, so inside hello, this is student.

The big ideas

What you need to know

Look left of the dot

In student.hello(), this is student. The object before the dot at call time is what this points to.

No dot, no owner

Call a function plainly, like hello(), and this is undefined in strict mode (modules and classes are strict).

Arrows borrow this

Arrow functions don't have their own this. They use the this of the code around them.

You can set it yourself

call, apply and bind let you choose this. bind returns a new function with this locked in.

See it in code

Walk through the code

js
1"use strict";2const student = {3  name: "Priya",4  hello() { return "Hi, I'm " + this.name; },5};6 7console.log(student.hello());   // "Hi, I'm Priya"8 9const lost = student.hello;10// lost();  // TypeError: Cannot read properties of undefined (reading 'name')11 12const fixed = student.hello.bind({ name: "Rahul" });13console.log(fixed());           // "Hi, I'm Rahul"

1/4A strict-mode object with a regular method that reads this.name.

Copying the method into lost cuts it off from student. There's no dot at call time, so this is undefined and reading .name fails.

Save yourself the pain

Common mistakes

Oops

Passing a method directly as a callback, like setTimeout(student.hello, 1000).

Do this instead

Wrap it so the dot stays: setTimeout(() => student.hello(), 1000), or pass student.hello.bind(student).

Oops

Writing object methods as arrow functions and expecting this to be the object.

Do this instead

Use the method shorthand hello() { } for object methods. Save arrows for callbacks inside them.

Your mission

Who is this?

~15 min

Quick check

const bike = { brand: "Royal Enfield", show() { return this.brand; } }; const scooty = { brand: "Honda", show: bike.show }; What does scooty.show() return?

Go deeper

  1. 1Call the same method directly and as a callback, and compare what this points to.
  2. 2Use an arrow function inside a method to keep the outer this.
  3. 3Read an explanation of call, apply and bind once so they aren't a surprise in interviews.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge