JS: “this” Binding

By Xah Lee. Date: . Last updated: .

What is the Purpose of “this”?

In JavaScript, when a function is called, it has a associated value called “this binding”. (Except arrow functions. [see JS: Arrow Function])

In function body, the “this binding” is represented by the keyword this.

The purpose of “this binding” is to allow function to implicitly work with a object. (without actually passing the object as argument.)

For example, when you call myArray.pop(), the function pop's “this binding” is myArray. That's how pop knows about myArray.

A function may or may not make use of “this binding”. For example, if a user defined function body does not contain this keyword, then the associated “this binding” does nothing.

What is the Value of “this”?

The “this binding” is designed for use with function only. Typically, for function call of the form obj.method_name(), where the function method_name's “this binding” will be obj.

When this is evaluated outsite a function, its value is the global object. In browser, it is the window object. [see JS: Browser Window Object, Document Object Model]

When this is used inside a function and the function is called, the value of this depends on how the function is called. Here's a summary:

Following are details.

“this” in Method Call

The most common use is calling a function as a method of some object. For example, myobj.f(). In such case, the value of this is the object that the method f is a property of. (the myobj)

// example showing what 「this」 is in a method call
let ob = { k1: function () { return this; }};
console.log(ob === ob.k1()); // true

“this” in Constructor Call (new f())

When a function is invoked as a constructor (For example, new F() or new o.F()), its this value is the newly created object. (similar to this = Object.create(F.prototype))

[see JS: Object.create]

[see JS: Operator “new”]

Note: as long as the new is used, such as new obj.m(), the value of this is still the newly created object, not obj.

// example of showing the value of 「this」 in a constructor call
const Ff = function () { this.k1 = 4; };
console.log ( new Ff() );
// { k1: 4 }

Here's a example of using a constructor that's a method of some object.

[see JS: Operator “new”]

“this” in Global Function Call

When a function is called in a global context and not as a method of another object, the this evaluates to undefined if strict mode is on, else it's the global object.

"use strict"
const ff = function () { return this; };
console.log(ff() === undefined); // true
// 「this」 is the global object 「window」, in non-strict mode, running in browser
let f2 = function () { return this; };
console.log( f2() === window); // true

[see JS: What's Strict Mode and How to Determine Strict Mode?]

“this” in Nested Functions

In a nested function (For example, g is nested inside f), when g is called inside f, the value of g's this is undefined if strict mode is on, else the global object.

// 「this」 is in nested function has value undefined, if under use strict
"use strict"

const ff = function () {
    const gg = function () {
        return this;
    return gg();

console.log(ff() === undefined); // true

// if in non strict mode, it returns the global object.
// example of testing what 「this」 is in nested function
"use strict"

let o = {
    ff:function () {
        const gg = function () {
        return this;
        return gg();

console.log(o.ff() === undefined); // true

// if in non strict mode, it returns the global object.

Call Function with any thisBinding

You can call a function with its this set to any object you want. You can do this by using the method “call” or “apply”. See: JS: Function Call, Apply, Bind.


ECMAScript® 2016 Language Specification#sec-function-environment-records

JS Constructor/Class

  1. “this” Binding
  2. What's Constructor?
  3. Property Key "prototype"
  4. Operator “new”
  5. Operator “instanceof”
  6. Property Key “constructor”
  7. Class
  8. Keyword “extends”
  9. Keyword “super”
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog