Understanding the confusing and handy “This” keyword in JavaScript
December 4, 2020

📌 The environment (or the scope) in which any JavaScript line is being executed is called “The Execution Context”.

The object that this variable refers to changes every time when the execution context is changed.

By default, the execution context is global which means that if a code is being executed as part of a simple function call then the this variable will refer to the global object.

📌 In the case of a browser, the global object is the window object.

In a Node.js environment, for example, a special object global will be the value of this.

Let’s take a look at the example below.

function foo () {
    console.log(this === window);


This proves that this here refers to the global object, which is the window object in our case.

Note that, if strict mode is enabled for any function then the value of this will be undefined because in strict mode global object refers to undefined in place of the window object.

For example,

function foo () {
    'use strict';
    console.log(this === window);


Let’s take a look at another example

function Person(first_name, last_name) {
    this.first_name = first_name;
    this.last_name = last_name;
    this.displayName = function() {

var john = new Person('John', 'Reid');


In the example above, when we call new on Person, JavaScript will create a new object inside the Person function and save it as this.

Then, the first_name, last_name and displayName properties will be added on the newly created this object.

Let’s take a look at the same example we saw earlier.

function simpleFunction () {
    console.log("Simple function call")
    console.log(this === window); 

“Simple function call”

In the example above, the this keyword will refer to the global object, in our case, it’s the window object.

Let’s create a simple object.

let user = {
    count: 10,
    simpleFunction: simpleFunction,
    anotherFunction: function() {
        console.log(this === window);

Now, we have a simpleFunction property referring to our simpleFunction function.

We have also added another property as a method called anotherFunction.

If we call user.simpleFunction(), we will get the following in our console’s output:

“Simple function call”


It happens because simpleFunction() is now a property of the user object, then the this keyword will refer to the user object and not the global object in this case.

If we call user.anotherFunction() now, we should expect the same thing too.

Our this keyword will refer to the user object. So, console.log(this === window); should return false and we should get the following output in our console:


If just do the following, the output will be different.

let myFunction = user.anotherFunction;


In the example above, we do a simple function call.

As we already know by now, if a method is invoked as a simple function, then the this keyword will refer to the global object which is in our case equals to the window object, and hence console.log(this === window); will print true.



Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript Mar 3 Window setInterval() Method in JavaScript Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS

You might also like these

Containers 101: What are containers?MiscHow to add new elements with swing animation using JavaScript and CSS?CSSSolution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software”MiscUnary Operators in JavaScriptJavaScriptCanvas Drawing in HTML5HTMLHow to insert Bootstrap 4 in Angular 9 app?Angular