DEV Community

Cover image for 6 simple rules to fully understand "this" keyword in JavaScript
Denis Veleaev
Denis Veleaev

Posted on

6 simple rules to fully understand "this" keyword in JavaScript

Hey! πŸ‘‹
I'm Denis.

One of the most popular topics for a JavaScript interview is the this keyword. Even though this concept is fundamental, there are a lot of developers who don't really know the rules of this keyword.

Actually there is nothing complicated. I want to share with you 6 simple rules that will help you to stand out among the other interviewees.


1. new keyword

The first and the most important rule is that if the new keyword is used when we calling a function, inside this function this refers to a new object that is created and returned by the constructor function. This rule is also applicable when we use ES6 classes


function Person() {
  this.age = 23
const p = new Person()

/* Output */
// {}
// { age: 23 }

2. apply / call / bind

The rule that we all probably know is that when we can force a function call to use a particular object as this using one of these 3 methods: apply / call / bind

function myFunction() {

const  thisObject = {
  someValue: 23
}   // { someValue: 23 }
myFunction.apply(thisObject)  // { someValue: 23 }

const myBoundFunction = myFunction.bind(thisObject )
myBoundFunction()             // { someValue: 23 }

3. Function as a method

This one is quite popular for interviews. The rule is simple. When the function is called as a method (i.e., the function is called by a preceding dot), this is the object that the function is a property of (this is the object before that dot).

const thisObject = {
  someValue: 23,
  itsMethod: function() {

thisObject.itsMethod('John') // { someValue: 23, itsMethod: Ζ’ }

4. The simplest case

This is the simplest one, but a lot of people forget (or don't know) it. The rule says: if the function is called without any of the conditions above, this is the global object (window) for browser.

function f() {

f() // Window (global object)

5. The order

If multiple rules can be applied simultaneously, the rule that is higher in the list will determine the this value.

6. Arrow functions

Arrow functions are the most favorite for interviews. Everyone knows that there's something special with this and arrow functions, but the minority really can tell the rule.

So, the rule is that arrow function ignores all the rules and takes the this value of its surrounding scope at the time it’s created.

It's that simple! We can determine an arrow function's this value by finding the line where it was created and looking at what the value of this is there.

Test yourself!

Let's test if you understand the rules πŸ™‚.

const obj = {
  value: 'abc',
  createFn: function() {
    return function() {
const fn = obj.createFn()

So, can you tell me what's going to be printed as this in this case? Can you clearly tell why?

To test it you can simply run this code in chrome console.

But can you answer how to "fix" this code?

If the answer is "Yes", congratulations! πŸŽ‰ You're one step closer to passing JavaScript interview!


The rules of this are really simple, but knowing them you can definitely show understanding of the fundamentals to the interviewer.

Thanks a lot for reading my article. Feel free to subscribe to me on DEV and Twitter @DenisVeleaev.


Top comments (2)

skaytech profile image

Great Article Dennis! You kept it concise with practical examples. Well done :-)
I did write about the same topic a few weeks back, which was much more detailed. Do check it out ->

srikanth597 profile image

To fix it u can do any of this.
access it via closure instead of directly in the nested anonymous function i.e let that=this.
or manually apply bind(this) to nested function