DEV Community

Dan-Vy Le
Dan-Vy Le

Posted on • Updated on


More JavaScript Questions 101(MJSQ 101): this

More JavaScript Questions 101(MJSQ 101):

Continuing from my original blog post, I give you more javascript 101 questions!:

Today's blog will be dedicated to answering how this works.

this can produce many different results and a lot of it is case dependent. Because you're in the world of JS, a great way to determine what this is, is to console.log(this) to check. BUT, if you want to understand the background work that is happening, please read on.

Arnav Aggarwal has a great blog explaining how this works. Much of the examples below are taken directly from his blog.

The highest rule of all is that this is determined when a function is invoked (it's call site) The remaining rules in order of precedence is showcased below:

1. If new keyword is used when calling a function, this is a brand new object.

function forExample() {
    this.value = "football";

new forExample();
//output: {}
//output: {value: 'football}

2. If apply, call, or bind are used to call a function, this inside the function is the object(in this case, touchdown) that is passed in as the argument.

function score() {
var touchdown = {
    value: 6
var bindscore = score.bind(touchdown);
bindscore();     // output: -> { value: 6 };  // output:  { value: 6 }
score.apply(touchdown); // -> output: { value: 6 }

3. If a function is called as a method, this is the object. When a dot is to the left of the function invoked, this is the object to the left of the dot.

var obj = {
    value: 6,
    printThis: function() {

//output: {value: 5, printThis: function()}

4. If a function is invoked as a free function invocation, (global), this is the global object. In a browser, it's the window.

function fn() {
// If called in browser:
fn(); // output: Window {stop: ƒ, open: ƒ, alert: ƒ, ...}

To note: If multiple scenarios exist, the order of the previous mentioned conditions are important in establishing this.

5. If the function is an ES2015 arrow function, all rules are thrown out the window. To determine this, go a line above the arrow function's creation and see what this is there. It will be the same in the arrow function.

const obj = {
    value: 'Chiefs win superbowl.',
    createArrowFn: function() {
        return () => console.log(this);
const arrowFn = obj.createArrowFn();
// output: { value: 'Chiefs win superbowl.', createArrowFn: ƒ }

Hope this explains this better for you!

That's all for today, please leave any comments/questions/corrections in the comments. Thanks!


Huge huge thanks to github user: yangshun for aggregating the most popular JS, CSS and HTML questions and giving us his answers to it. My weekly blog posts are to go over several questions at a time to reinforce my knowledge of fundamental javascript as I grow my expertise in it. Many of my blog will be paraphrasing if not direct quote from his github. Find his tech interview handbook here and please support him!

And an additional thank you to Flatiron alum: Marissa O. who is a badass developer at Forbes magazine for directing me to his blog!

Other sources:

Top comments (0)

11 Tips That Make You a Better Typescript Programmer


1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields


Read the whole post now!