JavaScript Tutorial

JavaScript this

this is the object the current function was called on. In a method it is the owner. In a plain function it is not.

A value, not a declaration

this is not a variable you create. The engine sets it for each call. The same function can see a different this depending on how you call it: as a method, as a plain function, or as an event handler.

The useful picture for beginners: this is “the object to the left of the dot” when you write obj.method().

In a method it is the owner

Call a function as a property of an object and this is that object. Methods use this to read sibling fields without passing the object as an extra argument.

Example

const account = {
  owner: "Ada",
  balance: 40,
  label() {
    return this.owner + " has " + this.balance;
  },
};

console.log(account.label());

Inside label, this.owner is "Ada" because the call wasaccount.label(). The owner is account.

Click Try it in JavaScript under an example. That opens/javascript/try — a live page and a console.

In a plain function it is not

Call the same kind of function without an object in front and this is not that owner. In the browser, a loose function often sees the global window object (or undefined in strict mode). It will not magically remember an object you used earlier.

Example

"use strict";

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

const box = {
  id: 1,
  showThis: showThis,
};

box.showThis();
showThis();

box.showThis() logs the object. showThis() logsundefined in strict mode. Detaching a method — assigning it to a name and calling that name — is the usual way people lose this.

A nested function is a new call

A function inside a method does not inherit this from the method. It is a separate call. Save the owner in a const, or use an arrow (next chapter), if the inner function needs the same object.

Example

const cart = {
  items: ["pen", "ink"],
  summary() {
    const owner = this;
    function line() {
      return owner.items.length + " items";
    }
    return line();
  },
};

console.log(cart.summary());

const owner = this freezes the method’s object for the inner function. Ifline used this.items instead, it would not see the cart.

On a DOM event

In a classic handler, this is the element the listener is attached to. That is why old examples write this.textContent inside onclick. Preferevent.currentTarget when you want the same idea without relying onthis.

Example

<button id="go">Count</button>
<p id="out">0</p>
<script>
  const button = document.getElementById("go");
  const out = document.getElementById("out");
  button.addEventListener("click", function () {
    const n = Number(out.textContent) + 1;
    out.textContent = String(n);
    this.textContent = "Count (" + n + ")";
  });
</script>

Here this is the button. An arrow handler would not set this to the button — that is the next lesson.

call, apply, and bind

You can set this on purpose. fn.call(obj) runs fn withthis equal to obj. fn.bind(obj) returns a new function that always uses that object. You need these later, when you pass methods as callbacks.

Call stylethis
obj.method()obj
method() (detached)undefined in strict mode
DOM function handlerThe element
Arrow functionInherited from outside — no own this

What to remember

  • this depends on the call, not on where the function was written.
  • A method sees its owner. A plain function does not.
  • Copying a method into a variable drops the owner.
  • Inner functions need a saved const, bind, or an arrow.

Next: arrow functions — short syntax, and the rule that they do not bind their own this.

FAQ: JavaScript this

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript this examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript this in this JavaScript JavaScript lesson (JavaScript this).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.