Quick Reference Table

Context this Refers To
Global Scope (Non-Strict Mode) window (Browser) / global (Node.js)
Global Scope (Strict Mode) undefined
Regular Function (Non-Strict Mode) window (Browser) / global (Node.js)
Regular Function (Strict Mode) undefined
Object Method The calling object
Constructor Function The new instance
Class Constructor The new instance
Event Handler (function) The DOM element that received the event
Event Handler (arrow function) Inherits this from enclosing scope
Arrow Function Inherits this from outer lexical scope
call(), apply(), bind() Explicitly sets this

Core Concepts

Global Context

console.log(this); // window (browser) | global (Node.js)

"use strict";
console.log(this); // undefined

Function Context

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

showThis(); // window (non-strict) | undefined (strict mode)

Object Method Context

const user = {
  name: "John",
  greet() {
    console.log(`Hello, I'm ${this.name}`);
  }
};

user.greet(); // "Hello, I'm John"

Constructor & Class Context

function User(name) {
  this.name = name;
}

const john = new User("John");
console.log(john.name); // "John"