DEV Community

Cover image for What is "this" in JavaScript?
Clinton N. Dreisbach for Momentum

Posted on

4 2

What is "this" in JavaScript?

Here's a question I often get in class:

May we spend a little bit more time going over the fundamentals of how this works and how to identify what it’s referring to? I’d love for someone who is a bit more familiar with the concept to explain it like I’m 5.

The simplest explanation of this is that it's defined when a function is called. If the function is an attribute of an object, and that object is used when calling it, like object.coolFunction(), then this is the object. If the function is not an attribute of an object, then this is null.

Check it out:

const person = {
  name: "Amiro",
  sayHello: function () {
    console.log("Hello from " + this.name)
  }
}
person.sayHello() // => prints "Hello from Amiro"
const sayHello = person.sayHello
sayHello() // what happens here? What is "this" when we call this function?

Here's where it gets super hard: callbacks. Here's an example. Assume there's a button element stored in the variable button.

button.addEventListener('click', person.sayHello)

When the button is clicked, the function stored in person.sayHello is called, but it's important to note that it's called like a function, not like an attribute of person. So this is null when you click the button.

There's two ways to set this permanently so that it's what you want it to be at all times: the method .bind() and using arrow functions.

.bind()

All functions have a method .bind() that takes one argument. .bind() returns a new function that does the same thing, but it will have the variable this bound to that argument. An example:

function sayHello() {
  console.log("hello from " + this.name)
}

const sayHelloFromAmiro = sayHello.bind({name: 'Amiro'})
sayHelloFromAmiro() // => prints "hello from Amiro"

{name: 'Amiro'} was bound to this when we called .bind({name: 'Amiro'}) on sayHello.

Arrow functions

When you make a function using the () => {} syntax, known as the arrow syntax, this is bound at the time you create the function. This is really useful if you have a function on an object you want to act as an event listener.

Think back to the example of a DOM button element called button. Here it is with arrow functions:

const person = {
  name: "Amiro",
  sayHello: () => {
    console.log("Hello from " + this.name)
  }
}
person.sayHello() // => prints "Hello from Amiro"
button.addEventListener('click', person.sayHello)

This time, we'll get what we expect when we click button.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay